public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new SportsNewsUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); var roleResult = await this._userManager.AddToRoleAsync(user, "User"); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); 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 InvokeAsync(HttpContext context, IServiceProvider provider) { var userManager = provider.GetService <UserManager <SportsNewsUser> >(); if (!userManager.Users.Any()) { var roleManager = provider.GetService <RoleManager <IdentityRole> >(); var adminRoleExists = roleManager.RoleExistsAsync("Administrator").Result; if (!adminRoleExists) { var result = roleManager.CreateAsync(new IdentityRole("Administrator")).Result; if (!result.Succeeded) { throw new InvalidOperationException(); } } var userRoleExists = roleManager.RoleExistsAsync("User").Result; if (!userRoleExists) { var result = roleManager.CreateAsync(new IdentityRole("User")).Result; if (!result.Succeeded) { throw new InvalidOperationException(); } } var user = new SportsNewsUser { Email = "*****@*****.**", UserName = "******", }; var userResult = await userManager.CreateAsync(user, "12345678"); if (!userResult.Succeeded) { throw new InvalidOperationException(); } var roleResult = await userManager.AddToRoleAsync(user, "Administrator"); if (!roleResult.Succeeded) { throw new InvalidOperationException(); } } await this._next(context); }
private async Task LoadSharedKeyAndQrCodeUriAsync(SportsNewsUser 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); }