private static string CreateImageUri(XenialIdentityUser user) { if (user.Picture != null && user.Picture.Length > 0 && !string.IsNullOrEmpty(user.PictureMimeType)) { return($"data:{user.PictureMimeType};base64,{Convert.ToBase64String(user.Picture)}"); } return(null); }
private async Task LoadAsync(XenialIdentityUser user) { var phone = await userManager.GetPhoneNumberAsync(user); Phone = phone; Input = new InputModel { NewPhone = phone, }; IsPhoneConfirmed = await userManager.IsPhoneNumberConfirmedAsync(user); }
private async Task LoadAsync(XenialIdentityUser user) { var email = await userManager.GetEmailAsync(user); Email = email; Input = new InputModel { NewEmail = email, }; IsEmailConfirmed = await userManager.IsEmailConfirmedAsync(user); }
public ProfilePictureModel(XenialIdentityUser user) { if (user == null) { return; } ImageUri = CreateImageUri(user); if (string.IsNullOrEmpty(ImageUri)) { BackColor = user.Color; ForeColor = MaterialColorPicker.ColorIsLight(user.Color) ? "var(--xenial-darker-color)" : "var(--xenial-lighter-color)"; } Inititals = user.Initials; }
private async Task LoadSharedKeyAndQrCodeUriAsync(XenialIdentityUser 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 static async Task RemoveClaimAsync(this UserManager <XenialIdentityUser> userManager, XenialIdentityUser user, string claimType) { var claims = await userManager.GetClaimsAsync(user); if (claims.Any(c => c.Type == claimType)) { var claim = claims.First(c => c.Type == claimType); await userManager.RemoveClaimAsync(user, claim); } }
public static async Task SetOrUpdateClaimAsync(this UserManager <XenialIdentityUser> userManager, XenialIdentityUser user, Claim newClaim) { var claims = await userManager.GetClaimsAsync(user); if (claims.Any(c => c.Type == newClaim.Type)) { var claim = claims.First(c => c.Type == newClaim.Type); await userManager.ReplaceClaimAsync(user, claim, newClaim); } else { await userManager.AddClaimAsync(user, newClaim); } }
public async Task <IActionResult> OnPostRegisterAsync(string returnUrl = null) { SelectedPage = "register"; returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); MarkAllFieldsAsSkipped(nameof(RegisterInput)); if (ModelState.IsValid) { //We don't have any users yet. First should be admin. const string AdminRole = "Administrator"; var shouldAddAdminRole = !await userManager.Users.AnyAsync(); if (shouldAddAdminRole) { if (!await roleManager.RoleExistsAsync(AdminRole)) { await roleManager.CreateAsync(new IdentityRole(AdminRole)); logger.LogInformation($"Created new '{AdminRole}' role."); } } var user = new XenialIdentityUser { UserName = RegisterInput.Email, Email = RegisterInput.Email }; var result = await userManager.CreateAsync(user, RegisterInput.Password); if (result.Succeeded) { logger.LogInformation("User created a new account with password."); if (shouldAddAdminRole) { var roleResult = await userManager.AddToRoleAsync(user, AdminRole); if (roleResult.Succeeded) { roleResult = await userManager.UpdateAsync(user); if (roleResult.Succeeded) { logger.LogInformation("Added '{User}' to the '{AdminRole}' role.", user, AdminRole); } } } 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 = user.Id, code = code, returnUrl = returnUrl }, protocol: Request.Scheme); await emailSender.SendEmailAsync(RegisterInput.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 = RegisterInput.Email, returnUrl = returnUrl })); } else { 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) { //We don't have any users yet. First should be admin. const string AdminRole = "Administrator"; var shouldAddAdminRole = !await userManager.Users.AnyAsync(); if (shouldAddAdminRole) { if (!await roleManager.RoleExistsAsync(AdminRole)) { await roleManager.CreateAsync(new IdentityRole(AdminRole)); logger.LogInformation($"Created new '{AdminRole}' role."); } } var user = new XenialIdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await userManager.CreateAsync(user); if (result.Succeeded) { if (shouldAddAdminRole) { var roleResult = await userManager.AddToRoleAsync(user, AdminRole); if (roleResult.Succeeded) { roleResult = await userManager.UpdateAsync(user); if (roleResult.Succeeded) { logger.LogInformation("Added '{User}' to the '{AdminRole}' role.", user, AdminRole); } } } result = await userManager.AddLoginAsync(user, info); if (result.Succeeded) { 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>."); // If account confirmation is required, we need to show the link if we don't have a real email sender if (userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email })); } await signInManager.SignInAsync(user, isPersistent : false, info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } ProviderDisplayName = info.ProviderDisplayName; ReturnUrl = returnUrl; return(Page()); }
private async Task <XenialIdentityUser> AutoProvisionUserAsync(string provider, string providerUserId, IEnumerable <Claim> claims) { // create a list of claims that we want to transfer into our store var filtered = new List <Claim>(); // user's display name var name = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Name)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value; if (name != null) { filtered.Add(new Claim(JwtClaimTypes.Name, name)); } else { var first = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.GivenName)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value; var last = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.FamilyName)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value; if (first != null && last != null) { filtered.Add(new Claim(JwtClaimTypes.Name, first + " " + last)); } else if (first != null) { filtered.Add(new Claim(JwtClaimTypes.Name, first)); } else if (last != null) { filtered.Add(new Claim(JwtClaimTypes.Name, last)); } } // email var email = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Email)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value; if (email != null) { filtered.Add(new Claim(JwtClaimTypes.Email, email)); } var user = new XenialIdentityUser { UserName = Guid.NewGuid().ToString(), }; var identityResult = await _userManager.CreateAsync(user); if (!identityResult.Succeeded) { throw new Exception(identityResult.Errors.First().Description); } if (filtered.Any()) { identityResult = await _userManager.AddClaimsAsync(user, filtered); if (!identityResult.Succeeded) { throw new Exception(identityResult.Errors.First().Description); } } identityResult = await _userManager.AddLoginAsync(user, new UserLoginInfo(provider, providerUserId, provider)); if (!identityResult.Succeeded) { throw new Exception(identityResult.Errors.First().Description); } return(user); }