private void CheckUser(DealershipUser user) { if (user == null) { throw new ArgumentException(); } }
public void DemoteAdmin(DealershipUser user) { this.CheckUser(user); this.userManager.RemoveFromRoleAsync(user, Constants.AdminRole).Wait(); this.userManager.AddToRoleAsync(user, Constants.UserRole).Wait(); }
private async Task LoadSharedKeyAndQrCodeUriAsync(DealershipUser user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new DealershipUser { UserName = Input.Username, Email = Input.Email, FirstName = Input.FirstName, MiddleName = Input.MiddleName, LastName = Input.LastName }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new DealershipUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public async Task InvokeAsync(HttpContext httpContext, IServiceProvider serviceProvider, SignInManager <DealershipUser> signInManager) { // Seed Roles var roleManager = serviceProvider.GetService <RoleManager <IdentityRole> >(); var adminRoleExists = roleManager.RoleExistsAsync(Constants.AdminRole).Result; if (!adminRoleExists) { await roleManager.CreateAsync(new IdentityRole(Constants.AdminRole)); } var userRoleExists = roleManager.RoleExistsAsync(Constants.UserRole).Result; if (!userRoleExists) { await roleManager.CreateAsync(new IdentityRole(Constants.UserRole)); } // Seed User admin if (!signInManager.UserManager.Users.Any()) { var user = new DealershipUser { UserName = AdminUsername, Email = AdminEmail, FirstName = AdminFirstName, LastName = AdminLastName, RegisteredOn = DateTime.Now }; var result = signInManager.UserManager.CreateAsync(user, AdminPassword).Result; var roleResult = signInManager.UserManager.AddToRoleAsync(user, Constants.AdminRole).Result; } await this.next(httpContext); }