public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { 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 OgmaUser { 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); var userId = await _userManager.GetUserIdAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = userId, 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>."); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
private async Task LoadAsync(OgmaUser user) { var email = await _userManager.GetEmailAsync(user); Email = email; Input = new InputModel { NewEmail = email, }; IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user); }
private async Task LoadAsync(OgmaUser user) { var userName = await _userManager.GetUserNameAsync(user); var userTitle = await _userManager.GetTitleAsync(user); var userBio = await _userManager.GetBioAsync(user); Username = userName; Input = new InputModel { Title = userTitle, Bio = userBio, }; }
private async Task LoadSharedKeyAndQrCodeUriAsync(OgmaUser 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 ??= Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (!ModelState.IsValid) { return(Page()); } // Check ReCaptcha var reResponse = await _reCaptcha.Validate(ReCaptchaResponse); if (!reResponse.success) { ModelState.TryAddModelError("ReCaptcha", "Incorrect ReCaptcha"); return(Page()); } // Check if invite code is correct var inviteCode = await _context.InviteCodes .FirstOrDefaultAsync(ic => ic.NormalizedCode == Input.InviteCode.ToUpper()); if (inviteCode == null) { ModelState.TryAddModelError("InviteCode", "Incorrect invite code"); return(Page()); } if (inviteCode.UsedDate != null) { ModelState.TryAddModelError("InviteCode", "This invite code has been used"); return(Page()); } // Create user var user = new OgmaUser { UserName = Input.Name, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); // If everything went fine... if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); // Modify invite code inviteCode.UsedBy = user; inviteCode.UsedDate = DateTime.Now; await _context.SaveChangesAsync(); // Setup default blacklists var defaultBlockedRatings = await _context.Ratings .Where(r => r.BlacklistedByDefault) .AsNoTracking() .ToListAsync(); var blockedRatings = defaultBlockedRatings.Select(dbr => new BlacklistedRating { User = user, Rating = dbr }); await _context.BlacklistedRatings.AddRangeAsync(blockedRatings); // Send confirmation code var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", null, new { area = "Identity", userId = user.Id, code }, Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email })); } else { 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 static string UserRoleAdded(OgmaUser user, string modName, string roleName) => $"User **{user.UserName}** (id: {user.Id}) has been granted a **{roleName}** role by **{modName}**.";
// Role templates public static string UserRoleRemoved(OgmaUser user, string modName, string roleName) => $"User **{user.UserName}** (id: {user.Id}) had their role **{roleName}** removed by **{modName}**.";
public static string UserUnmute(OgmaUser user, string modName, DateTime until) => $"User **{user.UserName}** (id: {user.Id}) has been unmuted {(until - DateTime.Now).HumanizeTimespan()} early by **{modName}**.";
// Mute templates public static string UserMute(OgmaUser user, string modName, DateTime until) => $"User **{user.UserName}** (id: {user.Id}) has been muted until {until} by **{modName}**.";