private async Task LoadSharedKeyAndQrCodeUriAsync(ParagraphUser 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); }
private void SetDatabaseForGetUserAndAdminRequests() { var user = new ParagraphUser { Id = "solomon", UserName = "******" }; this.context.Users.Add(user); this.context.SaveChanges(); var role = new IdentityRole { Name = "Admin" }; this.roleManager.CreateAsync(role); this.userManager.AddToRoleAsync(user, "Admin"); var users = new List <ParagraphUser>() { new ParagraphUser { Id = "simon", UserName = "******" }, new ParagraphUser { Id = "salambo", UserName = "******" }, new ParagraphUser { Id = "sinbad", UserName = "******" } }; this.context.Users.AddRange(users); this.context.SaveChanges(); foreach (var u in users) { this.context.Requests.Add(new Request { RequestSenderId = u.Id, Role = role, RequestReceiverId = user.Id }); this.context.SaveChanges(); } }
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 ParagraphUser { 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 void TestIf_GetAllRoles_ReturnsCorrectNumberOfRoles() { var roles = new List <IdentityRole>() { new IdentityRole { Name = "Writer" }, new IdentityRole { Name = "User" }, new IdentityRole { Name = "Admin" } }; roles.ForEach(p => roleManager.CreateAsync(p)); var user = new ParagraphUser { UserName = "******" }; this.context.Users.Add(user); this.context.SaveChanges(); Assert.Single(this.requestService.GetAllRoles("Solomon")); Assert.Contains("Writer", this.requestService.GetAllRoles("Solomon")); foreach (var role in roles) { this.context.Requests.Add(new Request { Role = role, RequestSender = user }); } this.context.SaveChanges(); Assert.Empty(this.requestService.GetAllRoles("Solomon")); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new ParagraphUser { UserName = Input.Email, Email = Input.Email }; 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 _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); 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()); }