protected void CreateUser_Click(object sender, EventArgs e) { var company = _companyDal.Get(Company.Text); if (company != null) { ErrorMessage.Text = "Company already exists."; return; } var addedCompany = _companyDal.Create(new Company { CompanyName = Company.Text }); var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>(); var user = new AspNetUser { UserName = Email.Text, Email = Email.Text }; var result = manager.Create(user, Password.Text); if (result.Succeeded) { AddRoleAndCompany(user, addedCompany.CompanyId); signInManager.SignIn(user, false, false); IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void addUserSave_OnClick(object sender, EventArgs e) { var user = _userCache.Get(Context.User.Identity.GetUserId()); var error = ValidateNewUser(user); if (!string.IsNullOrEmpty(error)) { UserErrorMessage.Text = error; AddUserPopupExtender.Show(); return; } var company = _companyDal.Get(user.CompanyName); if (!string.IsNullOrEmpty(company.Domain)) { if (!UserEmailInput.Text.EndsWith(company.Domain)) { UserErrorMessage.Text = $"Unauthorized company email. Must include the domain: {company.Domain}."; AddUserPopupExtender.Show(); return; } } var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var newUser = new AspNetUser { UserName = UserEmailInput.Text, Email = UserEmailInput.Text }; var result = manager.Create(newUser, UserPasswordInput.Text); if (result.Succeeded) { AddRoleAndCompany(newUser, company.CompanyId); UserErrorMessage.Text = string.Empty; UserEmailInput.Text = string.Empty; UserPasswordInput.Text = string.Empty; UserConfirmInput.Text = string.Empty; RefreshGrid(); } else { AddUserPopupExtender.Show(); UserErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private void AddRoleAndCompany(AspNetUser user, int companyId) { _userDal.AddToRole(user.Id, Roles.CompanyUser.Id); _companyDal.AddUser(companyId, user.Id); }
private void CreateAndLoginUser() { if (!IsValid) { return; } var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var signInManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); var user = new AspNetUser { UserName = email.Text, Email = email.Text }; var result = manager.Create(user); if (result.Succeeded) { var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(); if (loginInfo == null) { RedirectOnFail(); return; } result = manager.AddLogin(user.Id, loginInfo.Login); if (result.Succeeded) { signInManager.SignIn(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // var code = manager.GenerateEmailConfirmationToken(user.Id); // Send this link via email: IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id) IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); return; } } AddErrors(result); }