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);
        }
        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);
        }
        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);
        }