protected void CreateUser_Click(object sender, EventArgs e) { string firstName = FirstName.Text; string lastName = LastName.Text; string userName = UserName.Text; string email = Email.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); ApplicationUser u = new ApplicationUser() { FirstName = firstName, LastName = lastName, UserName = userName, Email = email }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private void CreateAndLoginUser() { if (!IsValid) { return; } var manager = new UserManager(); var user = new ApplicationUser() { UserName = userName.Text }; IdentityResult result = manager.Create(user); if (result.Succeeded) { var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(); if (loginInfo == null) { Response.Redirect("~/Account/Login"); return; } result = manager.AddLogin(user.Id, loginInfo.Login); if (result.Succeeded) { IdentityHelper.SignIn(manager, user, isPersistent: false); IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); return; } } AddErrors(result); }
public static void SignIn(UserManager manager, ApplicationUser user, bool isPersistent) { IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication; authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }
public static UserViewModel GetUserViewModel(ApplicationUser user) { return new UserViewModel() { Id = user.Id, UserName = user.UserName, Email = user.Email }; }
protected void CreateUser_Click(object sender, EventArgs e) { var manager = new UserManager(); var user = new ApplicationUser() { UserName = UserName.Text, FirstName = this.FirstName.Text, LastName = this.LastName.Text, Email = this.Email.Text }; IdentityResult result = manager.Create(user, Password.Text); if (result.Succeeded) { IdentityHelper.SignIn(manager, user, isPersistent: false); IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private async void CreateUser(string userName, string password, string firstName, string lastName, string email) { var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); var role = await manager.Roles.FindRoleByNameAsync("Registered user"); if (role == null) { ErrorMessage.Text = "Role \"Registered user\" missing."; return; } ApplicationUser user = new ApplicationUser() { UserName = userName, FirstName = firstName, LastName = lastName, Email = email }; IdentityResult result = manager.Users.CreateLocalUser(user, password); if (result.Success) { IdentityResult addToRoleResult = await manager.Roles.AddUserToRoleAsync(user.Id, role.Id); if (addToRoleResult.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, user.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = addToRoleResult.Errors.FirstOrDefault(); } } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private void CreateAndLoginUser() { if (!IsValid) { return; } var user = new ApplicationUser(); IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication; IdentityResult result = manager.CreateAndSignInExternalUser(Context.GetOwinContext().Authentication, user); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { AddErrors(result); return; } }