protected void CreateUser_Click(object sender, EventArgs e) { var manager = this.Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var signInManager = this.Context.GetOwinContext().Get<ApplicationSignInManager>(); var user = new User() { UserName = this.UserName.Text, FirstName = this.FirstName.Text, LastName = this.LastName.Text, Email = this.Email.Text }; IdentityResult result; try { result = manager.Create(user, this.Password.Text); } catch (DbEntityValidationException ex) { this.ErrorMessage.Text = string.Empty; foreach (var entityValidationError in ex.EntityValidationErrors) { foreach (var validationError in entityValidationError.ValidationErrors) { this.ErrorMessage.Text += validationError.ErrorMessage + " "; } } return; } if (result.Succeeded) { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 //string code = manager.GenerateEmailConfirmationToken(user.Id); //string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request); //manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>."); signInManager.SignIn( user, isPersistent: false, rememberBrowser: false); IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response); } else { this.ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
internal void AddUserAndRole() { // Access the application context and create result variables. var context = new SummitCommunityDbContext(); IdentityResult IdRoleResult; IdentityResult IdUserResult; // Create a RoleStore object by using the ApplicationDbContext object. // The RoleStore is only allowed to contain IdentityRole objects. var roleStore = new RoleStore<IdentityRole>(context); // Create a RoleManager object that is only allowed to contain IdentityRole objects. // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. var roleMgr = new RoleManager<IdentityRole>(roleStore); // Then, you create the "canEdit" role if it doesn't already exist. if (!roleMgr.RoleExists("admin")) { IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" }); } // Create a UserManager object based on the UserStore object and the ApplicationDbContext // object. Note that you can create new objects and use them as parameters in // a single line of code, rather than using multiple lines of code, as you did // for the RoleManager object. var userMgr = new UserManager<User>(new UserStore<User>(context)); var user = new User { FirstName = "John", LastName = "Doe", UserName = "******", Email = "*****@*****.**" }; IdUserResult = userMgr.Create(user, "123456"); // If the new "canEdit" user was successfully created, // add the "canEdit" user to the "canEdit" role. if (!userMgr.IsInRole(userMgr.FindByName("admin").Id, "admin")) { IdUserResult = userMgr.AddToRole(userMgr.FindByName("admin").Id, "admin"); } }
private void CreateAndLoginUser() { if (!IsValid) { return; } var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var signInManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); var user = new User() { UserName = email.Text, Email = email.Text }; IdentityResult 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); }