コード例 #1
0
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            try
            {
                using (ApplicationDbContext ctx = new ApplicationDbContext())
                {
                    //var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    var manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(ctx));
                    Logic.CustomUserValidator customerUserValidator = new Logic.CustomUserValidator();
                    manager.UserValidator = customerUserValidator;
                    var user = new ApplicationUser()
                    {
                        UserName              = txtUsername.Text,
                        FirstName             = txtFirstName.Text,
                        LastName              = txtLastName.Text,
                        Email                 = txtEmail.Text,
                        PhoneNumber           = txtPhone.Text,
                        Status                = ctx.UserStatuses.First(us => us.StatusDescription == "Active"),
                        HasBeenDisassociated  = false,
                        DisassociatedMerchant = null
                    };

                    String randomPassword = CreatePassword();

                    IdentityResult result = manager.Create(user, randomPassword);

                    if (result.Succeeded)
                    {
                        lblNewUserMessage.Text = "";

                        ApplicationUser newUser = manager.FindByName(user.UserName);

                        if (newUser != null)
                        {
                            if (!manager.IsInRole(newUser.Id, "Merchant"))
                            {
                                manager.AddToRole(newUser.Id, "Merchant");
                                manager.Update(newUser);
                                ctx.SaveChanges();
                            }
                        }
                        else
                        {
                            lblNewUserMessage.Text = "An error occurred while adding the new user.  Please contact your system administrator before continuing.";
                        }

                        NewUserAdded();

                        btnContinueAddUser.Visible = true;
                        btnCancelAddUser.Visible   = false;
                        btnAddUser.Visible         = false;
                    }
                    else
                    {
                        lblNewUserMessage.Text = result.Errors.FirstOrDefault();
                    }
                }
            }
            catch (System.Exception ex)
            {
                _newLogic.WriteExceptionToDB(ex, "btnAddUser_Click");
            }
        }
コード例 #2
0
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            try
            {
                using (ApplicationDbContext ctx = new ApplicationDbContext())
                {
                    //var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ctx));
                    Logic.CustomUserValidator customerUserValidator = new Logic.CustomUserValidator();
                    manager.UserValidator = customerUserValidator;
                    var user = new ApplicationUser()
                    {
                        UserName = txtUsername.Text,
                        FirstName = txtFirstName.Text,
                        LastName = txtLastName.Text,
                        Email = txtEmail.Text,
                        PhoneNumber = txtPhone.Text,
                        Status = ctx.UserStatuses.First(us => us.StatusDescription == "Active"),
                        HasBeenDisassociated = false,
                        DisassociatedMerchant = null
                    };

                    String randomPassword = CreatePassword();

                    IdentityResult result = manager.Create(user, randomPassword);

                    if (result.Succeeded)
                    {
                        lblNewUserMessage.Text = "";

                        ApplicationUser newUser = manager.FindByName(user.UserName);

                        if (newUser != null)
                        {
                            if (!manager.IsInRole(newUser.Id, "Merchant"))
                            {
                                manager.AddToRole(newUser.Id, "Merchant");
                                manager.Update(newUser);
                                ctx.SaveChanges();
                            }

                        }
                        else
                        {
                            lblNewUserMessage.Text = "An error occurred while adding the new user.  Please contact your system administrator before continuing.";
                        }

                        NewUserAdded();

                        btnContinueAddUser.Visible = true;
                        btnCancelAddUser.Visible = false;
                        btnAddUser.Visible = false;
                    }
                    else
                    {
                        lblNewUserMessage.Text = result.Errors.FirstOrDefault();
                    }
                }
            }
            catch (System.Exception ex)
            {
                _newLogic.WriteExceptionToDB(ex, "btnAddUser_Click");
            }
        }