protected void CreateUser_Click(object sender, EventArgs e) { var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>(); var signInManager = Context.GetOwinContext().Get <ApplicationSignInManager>(); var userRole = Context.GetOwinContext().Get <ApplicationRoleManager>(); System.DateTime dt = System.DateTime.Parse(dob.Text); var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text, PhoneNumber = Phone.Text, FirstName = fname.Text, LastName = lname.Text, Dob = dt, IsAdmin = isadmin.Checked }; IdentityResult result = manager.Create(user, Password.Text); manager.AddToRole(user.Id, "Admin"); // should change to customer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! var res = userRole.Create(new ApplicationRole("Admin", user, 0)); if (result.Succeeded) { // For more information on how to enable account confirmation and password reset please visit https://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>."); GV.OnLogin(user); Session["userId"] = GV.user.Id; signInManager.SignIn(user, isPersistent: false, rememberBrowser: false); IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { // Validate the user password var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>(); var signinManager = Context.GetOwinContext().GetUserManager <ApplicationSignInManager>(); // This doen't count login failures towards account lockout // To enable password failures to trigger lockout, change to shouldLockout: true var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false); switch (result) { case SignInStatus.Success: ApplicationUserManager manager1 = Context.GetOwinContext().GetUserManager <ApplicationUserManager>(); Session["userId"] = GV.OnLogin(manager1.FindByEmail(Email.Text)); IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); break; case SignInStatus.LockedOut: Response.Redirect("/Account/Lockout"); break; case SignInStatus.RequiresVerification: Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", Request.QueryString["ReturnUrl"], RememberMe.Checked), true); break; case SignInStatus.Failure: default: FailureText.Text = "Invalid login attempt"; ErrorMessage.Visible = true; break; } } }