public async Task <IActionResult> OnPostAsync( [FromServices] UserManager <IdentityUser> userManager, [FromServices] SignInManager <IdentityUser> signInManager) { if (!ModelState.IsValid) { return(Page()); } var user = new IdentityUser(Form.Username) { Email = Form.Email }; var createUserResult = await userManager.CreateAsync(user, Form.Password); if (createUserResult.Succeeded) { await signInManager.SignInAsync(user, true); return(Redirect(Form.ReturnUrl)); } foreach (var error in createUserResult.Errors) { CustomErrors.Add(error.Description); } return(Page()); }
public async Task <IActionResult> OnPostAsync( [FromServices] UserManager <IdentityUser> userManager, [FromServices] SignInManager <IdentityUser> signInManager) { if (!ModelState.IsValid) { return(Page()); } var existingUser = await userManager.FindByNameAsync(Form.Username); if (existingUser != null) { CustomErrors.Add("Username already taken."); return(Page()); } var user = await userManager.FindByNameAsync(Form.Email); var resetPasswordResult = await userManager.ResetPasswordAsync(user, Form.Code, Form.Password); if (resetPasswordResult.Succeeded) { user.UserName = Form.Username; await userManager.UpdateAsync(user); await signInManager.SignInAsync(user, true); return(Redirect(Form.ReturnUrl)); } CustomErrors.Add("Failed to create account."); return(Page()); }
public async Task <IActionResult> OnPostAsync([FromServices] SignInManager <IdentityUser> signInManager) { if (!ModelState.IsValid) { return(Page()); } var signInResult = await signInManager .PasswordSignInAsync(Form.Username, Form.Password, true, false); if (signInResult.Succeeded) { return(Redirect(Form.ReturnUrl)); } CustomErrors.Add("Invalid login attempt, please try again."); return(Page()); }
// Method for registering in -> when we submit the form // DI at method level, injecting SignInManager & UserManager for User // UserManager is a service -> come from Startup -> AddIdentity // SignInManager is a service -> come from Startup -> AddIdentity public async Task <IActionResult> OnPostAsync( [FromServices] UserManager <IdentityUser> userManager, [FromServices] SignInManager <IdentityUser> signInManager ) { // If register form is invalid, return that page again => user did not provide valid email || password if (!ModelState.IsValid) { return(Page()); } // Created a User, with Username -> what he typed in the form var user = new IdentityUser(Form.Username) { // Adding email from form to user object as-well Email = Form.Email }; // Creates user in store with user manager, based on identity (username included) user and password from form var createUserResult = await userManager.CreateAsync(user, Form.Password); // If creation succeeded if (createUserResult.Succeeded) { // Sign the User in await signInManager.SignInAsync(user, true); // If signInResult succeeded we want to pop user back on from where he came from, returnUrl contains all the info // that server need to redirect us back to the App return(Redirect(Form.ReturnUrl)); } // Loop over errors in createUserResult -> when we try to create user and he fked up or didnt filled form correctly foreach (var error in createUserResult.Errors) { // Adding to our List of errors, error (it's description -> describes what error is) that popped for the reason above CustomErrors.Add(error.Description); } return(Page()); }
// Method for loging in -> when we submit the form => press the Log In button // DI at method level, injecting SignInManager for User // SignInManager is a service -> come from Startup -> AddIdentity public async Task <IActionResult> OnPostAsync([FromServices] SignInManager <IdentityUser> signInManager) { // If login form is invalid, return that page again => user did not provide valid username || password if (!ModelState.IsValid) { return(Page()); } // Attempts to sign in the specified userName and password combination, if we close it's gonna persist, no lockout on failures // Providing username and pass from form to -> PasswordSignInAsync var signInResult = await signInManager.PasswordSignInAsync(Form.Username, Form.Password, true, false); if (signInResult.Succeeded) { return(Redirect(Form.ReturnUrl)); } // Add this particular error to our List, dont provide details coz of hacking possibilities CustomErrors.Add("Invalid login attempt, please try again."); return(Page()); }