public virtual async Task UpdateNormalizedTenantNameAsync(TTenant tenant) { var tenantName = await GetTenantNameAsync(tenant); var normalizedName = ProtectPersonalData(KeyNormalizer.MaybeNormalizeName(tenantName)); await _tenantStore.SetNormalizedTenantNameAsync(tenant, normalizedName, CancellationToken); }
public virtual async Task UpdateNormalizedApplicationNameAsync(TApplication application) { var applicationName = await GetApplicationNameAsync(application); ; var normalizedName = ProtectPersonalData(KeyNormalizer.MaybeNormalizeName(applicationName)); await _applicationStore.SetNormalizedApplicationNameAsync(application, normalizedName, CancellationToken); }
protected virtual string NormalizeKey(TCacheKey key) { return(KeyNormalizer.NormalizeKey( new DistributedCacheKeyNormalizeArgs( key.ToString(), CacheName, IgnoreMultiTenancy ) )); }
public virtual async Task <IEnumerable <TTenant> > FindByEmailAsync(string email) { ThrowIfDisposed(); if (email == null) { throw new ArgumentNullException(nameof(email)); } email = KeyNormalizer.MaybeNormalizeName(email); if (!(_userStore is IUserEmailStoreExtended <TUser> emailStore)) { throw new NotSupportedException(); } var users = await emailStore.FindAllByEmailAsync(email, CancellationToken); if (users != null || !Options.Stores.ProtectPersonalData) { if (!(users is IEnumerable <IdentityUserExtended <TKey> > ext)) { throw new NotSupportedException(); } var tenantIds = ext.Select(x => $"{x.TenantId}"); return(await _tenantStore.FindByIdsAsync(tenantIds, CancellationToken)); } var protector = _serviceProvider.GetService(typeof(ILookupProtector)) as ILookupProtector; if (!(_serviceProvider.GetService(typeof(ILookupProtectorKeyRing)) is ILookupProtectorKeyRing service) || protector == null) { return(null); } foreach (var allKeyId in service.GetAllKeyIds()) { users = await emailStore.FindAllByEmailAsync(protector.Protect(allKeyId, email), CancellationToken); if (users is IEnumerable <IdentityUserExtended> ext) { var tenantIds = ext.Select(x => $"{x.TenantId}"); var tenants = await _tenantStore.FindByIdsAsync(tenantIds, CancellationToken); if (tenants != null) { return(tenants); } } } return(null); }
private async Task <RolePermissionCacheItem> GetRolePermissionCacheItemAsync(string roleId, string tenantId) { var cacheKey = roleId + "@" + (!string.IsNullOrEmpty(tenantId) ? tenantId : "default"); return(await cacheManager.GetRolePermissionCache().GetCancelableAsync(cacheKey, async(cancellationToken) => { var cacheItem = new RolePermissionCacheItem(roleId); var normalizedRoleId = KeyNormalizer.NormalizeName(roleId); var staticRole = options.Role.StaticRoles.FirstOrDefault(c => string.Equals(c.Id, normalizedRoleId, StringComparison.OrdinalIgnoreCase)); if (staticRole != null) { foreach (var permission in permissionStore.GetAll()) { if (staticRole.IsGrantedByDefault(permission)) { cacheItem.GrantedPermissions.Add(permission.Name); } } } else { var role = await Store.FindByIdAsync(normalizedRoleId, cancellationToken); if (role == null) { throw new StructureException("There is no role with given id: " + normalizedRoleId); } } if (Store is IRolePermissionStore <TRolePermission> rolePermissionStore) { foreach (var rolePermission in await rolePermissionStore.GetPermissionsAsync(normalizedRoleId, cancellationToken)) { if (rolePermission.IsGranted) { cacheItem.GrantedPermissions.AddIfNotContains(rolePermission.PermissionName); } else { cacheItem.GrantedPermissions.Remove(rolePermission.PermissionName); } } } return cacheItem; })); }
public async Task <IActionResult> RegisterConfirmation(string email, string returnUrl = null) { if (email == null) { return(RedirectToAction("Index", "Home")); } var user = await UserEmailStore.FindByEmailAsync(KeyNormalizer.NormalizeEmail(email), CancellationToken.None); if (user == null) { return(NotFound($"Unable to load user with email '{email}'.")); } ViewData["Email"] = email; // TODO : remove this code that confirms the account var displayConfirmAccountLink = true; ViewData["DisplayConfirmAccountLink"] = displayConfirmAccountLink; if (displayConfirmAccountLink) { var userId = await UserManager.GetUserIdAsync(user); var code = await UserManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var emailConfirmationUrl = Url.Action ( "ConfirmEmail", "Account", new { userId, code, returnUrl }, protocol: Request.Scheme ); ViewData["EmailConfirmationUrl"] = emailConfirmationUrl; } return(View("RegisterConfirmation")); }
public virtual async Task <TApplication> FindByNameAsync(string applicationName) { ThrowIfDisposed(); if (applicationName == null) { throw new ArgumentNullException(nameof(applicationName)); } applicationName = KeyNormalizer.MaybeNormalizeName(applicationName); var application = await _applicationStore.FindByNameAsync(applicationName, CancellationToken); if (application != null || !Options.Stores.ProtectPersonalData) { return(application); } var protector = _serviceProvider.GetService(typeof(ILookupProtector)) as ILookupProtector; if (!(_serviceProvider.GetService(typeof(ILookupProtectorKeyRing)) is ILookupProtectorKeyRing service) || protector == null) { return(null); } foreach (var allKeyId in service.GetAllKeyIds()) { application = await _applicationStore.FindByNameAsync(protector.Protect(allKeyId, applicationName), CancellationToken); if (application != null) { return(application); } } return(null); }
/// <summary> /// Normalize a key (canonical name) for consistent comparisons. /// </summary> /// <param name="key">The key to normalize.</param> /// <returns>A normalized value representing the specified <paramref name="key"/>.</returns> public virtual string NormalizeKey(string key) { return (KeyNormalizer == null) ? key : KeyNormalizer.Normalize(key); }
public virtual string NormalizeKey(string key) { return(KeyNormalizer != null?KeyNormalizer.Normalize(key) : key); }
public async Task <IActionResult> Register(RegisterViewModel input) { if (ModelState.IsValid) { var id = CryptoRandom.CreateUniqueId(format: CryptoRandom.OutputFormat.Hex); var normalizedEmail = KeyNormalizer.NormalizeEmail(input.Email); var user = await UserStore.FindByNameAsync(normalizedEmail, CancellationToken.None) ?? new User { Id = id, UserName = input.Email, Email = input.Email, NormalizedEmail = normalizedEmail }; var result = default(IdentityResult); if (user.Id == id) { result = await UserManager.CreateAsync(user, input.Password); } else { result = IdentityResult.Failed(new IdentityError { Code = "USER_NOT_CREATED", Description = "Please login with your existing account; user already exists" }); } if (result.Succeeded) { var code = await UserManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page ( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code, returnUrl = input.ReturnUrl }, 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 (UserManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToAction("RegisterConfirmation", "Account", new { email = input.Email, returnUrl = input.ReturnUrl })); } else { await SignInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(input.ReturnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } return(View(input)); }
public virtual string?NormalizeKey(string?key) { return((KeyNormalizer == null) ? key : KeyNormalizer.NormalizeName(key)); }
/// <summary> /// Normalize domain for consistent comparisons. /// </summary> /// <param name="domain">The name to normalize.</param> /// <returns>A normalized value representing the specified <paramref name="domain"/>.</returns> public virtual string NormalizeDomain(string domain) => (KeyNormalizer == null) ? domain : KeyNormalizer.Normalize(domain);