コード例 #1
0
        public async Task <IdentityResult> ChangePasswordAsync(User user, string newPassword, CancellationToken token = default)
        {
            var store = this.Store as IUserPasswordStore <User>;

            if (store == null)
            {
                var errors = new IdentityError[]
                {
                    new IdentityError {
                        Description = "Current UserStore doesn't implement IUserPasswordStore"
                    }
                };

                return(IdentityResult.Failed(errors));
            }

            if (PasswordValidators.Any())
            {
                var tasks   = PasswordValidators.Select(x => x.ValidateAsync(this, user, newPassword));
                var results = await Task.WhenAll(tasks);

                if (results.Any(x => !x.Succeeded))
                {
                    return(results.First(x => !x.Succeeded));
                }
            }

            var newPasswordHash = PasswordHasher.HashPassword(user, newPassword);

            await store.SetPasswordHashAsync(user, newPasswordHash, token);

            return(IdentityResult.Success);
        }
コード例 #2
0
        public UserManager(IUserStore <TUser> store, IOptions <AuthOptions> options, IPasswordHasher <TUser> passwordHasher, IEnumerable <IUserValidator <TUser> > userValidators,
                           IEnumerable <IPasswordValidator <TUser> > passwordValidators, AuthErrorDescriber errors, IServiceProvider services, ILogger <UserManager <TUser> > logger)
        {
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }

            Store          = store;
            Options        = options?.Value ?? new AuthOptions();
            PasswordHasher = passwordHasher;
            ErrorDescriber = errors;
            Logger         = logger;

            if (userValidators != null)
            {
                foreach (var v in userValidators)
                {
                    UserValidators.Add(v);
                }
            }
            if (passwordValidators != null)
            {
                foreach (var v in passwordValidators)
                {
                    PasswordValidators.Add(v);
                }
            }

            _services = services;

            if (Options.Stores.ProtectPersonalData)
            {
                if (!(Store is IProtectedUserStore <TUser>))
                {
                    throw new InvalidOperationException("Store is not a protected user store!");
                }
                if (services.GetService <ILookupProtector>() == null)
                {
                    throw new InvalidOperationException("There's no personal data protector!");
                }
            }
        }