Ejemplo n.º 1
0
        public override async Task <IdentityResult> CreateAsync(ApplicationUser user, string password)
        {
            IUserPasswordStore <ApplicationUser> aa = Store as IUserPasswordStore <ApplicationUser>;

            if ((object)user == null)
            {
                //  return new IdentityResult(string.Format(SystemMessage.NewException, "CreateAsync User Null"));
                if (password == null)
                {
                    return(null);
                }
            }
            //   return new IdentityResult(string.Format(SystemMessage.NewException, "CreateAsync Password Null"));

            IdentityResult sssS = await this.UpdatePassword(aa, user, password);



            IdentityResult s = await base.CreateAsync(user);

            //await this.UpdateSecurityStampInternal(user);
            //IdentityResult identityResult = await this.UserValidator.ValidateAsync(user).WithCurrentCulture<IdentityResult>();
            //if (!identityResult.Succeeded)
            //    return identityResult;
            //if (this.UserLockoutEnabledByDefault && this.SupportsUserLockout)
            //    await this.GetUserLockoutStore().SetLockoutEnabledAsync(user, true).WithCurrentCulture();
            await this.Store.CreateAsync(user);

            //return IdentityResult.Success;

            //Store.CreateAsync(user);
            // var ss = _userService;
            return(null);
        }
Ejemplo n.º 2
0
        public virtual async Task <bool> CheckPasswordAsync(TUser user, string password)
        {
            ThrowIfDisposed();
            IUserPasswordStore <TUser> passwordStore = GetPasswordStore();

            if (user == null)
            {
                return(false);
            }

            PasswordVerificationResult result = await VerifyPasswordAsync(passwordStore, user, password);

            if (result == PasswordVerificationResult.SuccessRehashNeeded)
            {
                await UpdatePasswordHash(passwordStore, user, password, validatePassword : false);
                await UpdateUserAsync(user);
            }

            bool success = result != PasswordVerificationResult.Failed;

            if (!success)
            {
                Logger.LogWarning(LoggerEventIds.InvalidPassword, "Invalid password!");
            }

            return(success);
        }
Ejemplo n.º 3
0
        protected override Task <IdentityResult> UpdatePassword(IUserPasswordStore <ApplicationUser, string> passwordStore, ApplicationUser user, string newPassword)
        {
            // update the LastPasswordChangedDate field
            user.LastPasswordChangedDate = DateTime.Now;

            return(base.UpdatePassword(passwordStore, user, newPassword));
        }
Ejemplo n.º 4
0
 Task <PasswordVerificationResult> IAppUserManager.VerifyPasswordAsync(
     IUserPasswordStore <User> store,
     User user,
     string password)
 {
     return(base.VerifyPasswordAsync(store, user, password));
 }
Ejemplo n.º 5
0
        protected override async Task <bool> VerifyPasswordAsync(IUserPasswordStore <User, int> store, User user, string password)
        {
            string userEnteredPasswordHash = _encryptionService.CreatePasswordHash(password, user.PasswordSalt);

            return(userEnteredPasswordHash == user.PasswordHash);
            //return base.VerifyPasswordAsync(store, user, password);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Updates the user password.
        /// </summary>
        /// <param name="passwordStore">Unused implementation of UserPasswordStore.</param>
        /// <param name="user">User.</param>
        /// <param name="newPassword">New password in plain text format.</param>
        protected override async Task <IdentityResult> UpdatePassword(IUserPasswordStore <User, int> passwordStore, User user, string newPassword)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var result = await PasswordValidator.ValidateAsync(newPassword);

            if (!result.Succeeded)
            {
                return(result);
            }

            UserInfo userInfo = UserInfoProvider.GetUserInfo(user.Id);

            if (userInfo == null)
            {
                user.GUID         = Guid.NewGuid();
                user.PasswordHash = UserInfoProvider.GetPasswordHash(newPassword, UserInfoProvider.NewPasswordFormat, user.GUID.ToString());
            }
            else
            {
                UserInfoProvider.SetPassword(userInfo, newPassword);
                user.PasswordHash = ValidationHelper.GetString(userInfo.GetValue("UserPassword"), string.Empty);
                await UpdateSecurityStampInternal(user);
            }

            return(IdentityResult.Success);
        }
Ejemplo n.º 7
0
        public virtual async Task <AuthResult> ChangePasswordAsync(TUser user, string currentPassword, string newPassword)
        {
            ThrowIfDisposed();
            IUserPasswordStore <TUser> passwordStore = GetPasswordStore();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (await VerifyPasswordAsync(passwordStore, user, currentPassword) != PasswordVerificationResult.Failed)
            {
                AuthResult result = await UpdatePasswordHash(passwordStore, user, newPassword);

                if (!result.Succeeded)
                {
                    return(result);
                }

                return(await UpdateUserAsync(user));
            }

            Logger.LogWarning(LoggerEventIds.ChangePasswordFailed, "Password change failed!");
            return(AuthResult.Failed(ErrorDescriber.PasswordMismath()));
        }
Ejemplo n.º 8
0
        protected override Task <bool> VerifyPasswordAsync(IUserPasswordStore <User, int> store, User user, string password)
        {
            _passwordHasher.Salt = user.PasswordSalt;
            var result = _passwordHasher.VerifyHashedPassword(user.PasswordHash, password);

            return(Task.FromResult(result == PasswordVerificationResult.Success));
        }
Ejemplo n.º 9
0
 public void Init()
 {
     repository              = new Mock <IRepository <UserModel> >();
     userStore               = new UserStore(repository.Object);
     userStoreMethodLookup   = new UserStore(new TestRepository(new SqlMethodLookup()));
     cancellationTokenSource = new CancellationTokenSource();
 }
Ejemplo n.º 10
0
        protected override async Task <bool> VerifyPasswordAsync(IUserPasswordStore <User, int> store, User user,
                                                                 string password)
        {
            var hash = await store.GetPasswordHashAsync(user);

            return(hash == password);
        }
Ejemplo n.º 11
0
        protected virtual async Task <bool> VerifyPasswordAsync(IUserPasswordStore <ApplicationUser, Guid> store, ApplicationUser user,
                                                                string password)
        {
            var hash = await store.GetPasswordHashAsync(user).WithCurrentCulture();

            return(PasswordHasher.VerifyHashedPassword(hash, password) != PasswordVerificationResult.Failed);
        }
        public HomeController(
            ILogger <HomeController> logger,

            UserManager <User> userManager,
            RoleManager <Role> roleManager,
            SignInManager <User> signInManager,

            IUserStore <User> userStore,
            IRoleStore <Role> roleStore,

            IPasswordHasher <User> passwordHasher,
            IPasswordValidator <User> passwordValidator,

            IUserPasswordStore <User> passwordStore

            //IUserValidator<User> userValidator,
            //IRoleValidator<User> roleValidator
            )
        {
            Logger            = logger;
            UserManager       = userManager;
            RoleManager       = roleManager;
            SignInManager     = signInManager;
            UserStore         = userStore;
            RoleStore         = roleStore;
            PasswordHasher    = passwordHasher;
            PasswordValidator = passwordValidator;
            PasswordStore     = passwordStore;
            //UserValidator = userValidator;
            //RoleValidator = roleValidator;
        }
Ejemplo n.º 13
0
        internal async System.Threading.Tasks.Task <IdentityResult> ChangePasswordAsync(int userId, string newPassword)
        {
            AppUser user = await this.FindByIdAsync(userId).ConfigureAwait(false);

            if (user == null)
            {
                throw new InvalidOperationException("user not found");
            }

            IdentityResult result = await this.PasswordValidator.ValidateAsync(newPassword).ConfigureAwait(false);

            IUserPasswordStore <AppUser, int> pwdStore = (IUserPasswordStore <AppUser, int>) this.Store;

            if (result.Succeeded)
            {
                await pwdStore.SetPasswordHashAsync(user, newPassword).ConfigureAwait(false);
            }
            if (result.Succeeded)
            {
                result = await this.UpdateSecurityStampAsync(user.Id);
            }
            if (result.Succeeded)
            {
                result = await this.UpdateAsync(user);
            }

            return(result);
        }
 public NarikJwtLoginService(
     IUserPasswordStore <ApplicationUser> userPasswordStore,
     IPasswordHasher <ApplicationUser> passwordHasher, NarikModulesConfig config)
 {
     _userPasswordStore = userPasswordStore;
     _passwordHasher    = passwordHasher;
     _config            = config;
 }
Ejemplo n.º 15
0
 public ManagerIdentityUser(IUserPasswordStore <User, int> store)
     : base(store)
 {
     UserValidator = new UserValidator <User, int>(this)
     {
         AllowOnlyAlphanumericUserNames = false,
         //RequireUniqueEmail = true
     };
 }
Ejemplo n.º 16
0
        public void SetSpecificationContext()
        {
            _passwordStore = RavenUserStore as IUserPasswordStore <RavenIdentityUser>;

            if (_passwordStore == null)
            {
                Assert.Ignore();
            }
        }
Ejemplo n.º 17
0
        private async Task <IdentityResult> UpdatePasswordHash(IUserPasswordStore <TUser> passwordStore,
                                                               TUser user, string newPassword, bool validatePassword = true)
        {
            var hash = newPassword != null?PasswordHasher.HashPassword(user, newPassword) : null;

            await passwordStore.SetPasswordHashAsync(user, hash, CancellationToken);

            // await UpdateSecurityStampInternal(user);
            return(IdentityResult.Success);
        }
Ejemplo n.º 18
0
        protected async override Task <bool> VerifyPasswordAsync(IUserPasswordStore <AppUser, int> store, AppUser user, string password)
        {
            //base.VerifyPasswordAsync 使用的是 Rfc2898DeriveBytes 密码,
            //而这里传入的 password , 即上面的 CheckPasswordAsync 密码,未加密
            //return await base.VerifyPasswordAsync(store, user, password);

            var hash = await store.GetPasswordHashAsync(user);

            return(string.Equals(hash, password));
        }
Ejemplo n.º 19
0
        protected override async Task <bool> VerifyPasswordAsync(IUserPasswordStore <User, int> store, User user, string password)
        {
            var psUser = await store.FindByIdAsync(user.Id);

            if (psUser != null)
            {
                return(psUser.PasswordHash.Equals(password));
            }
            return(false);
        }
Ejemplo n.º 20
0
        protected override Task <bool> VerifyPasswordAsync(IUserPasswordStore <VistaUser, string> store, VistaUser user, string password)
        {
            //Logger.Log("ApplicationUserManager:VerifyPasswordAsync (user = {0}, password = {1})", user, password);
            //return base.VerifyPasswordAsync(store, user, password);

            bool retVal = false;

            retVal = user.PasswordHash == password;

            return(Task.FromResult <bool>(retVal));
        }
Ejemplo n.º 21
0
        private IUserPasswordStore <TUser> GetPasswordStore()
        {
            IUserPasswordStore <TUser> store = Store as IUserPasswordStore <TUser>;

            if (store == null)
            {
                throw new NotSupportedException("Store isn't a password store!");
            }

            return(store);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Verifies the user password.
        /// </summary>
        /// <param name="store">Unused implementation of UserPasswordStore.</param>
        /// <param name="user">User.</param>
        /// <param name="password">Password in plain text format.</param>
        protected override Task <bool> VerifyPasswordAsync(IUserPasswordStore <User, int> store, User user, string password)
        {
            if (user == null)
            {
                return(Task.FromResult(false));
            }

            var userInfo = UserInfoProvider.GetUserInfo(user.UserName);

            return(Task.FromResult(!UserInfoProvider.IsUserPasswordDifferent(userInfo, password)));
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Verifies the user password.
        /// </summary>
        /// <param name="store">Unused implementation of UserPasswordStore.</param>
        /// <param name="user">User.</param>
        /// <param name="password">Password in plain text format.</param>
        /// <returns><see langword="true"/> if <paramref name="password"/> matches the one in the database. Otherwise <see langword="false"/>.</returns>
        protected override Task <bool> VerifyPasswordAsync(IUserPasswordStore <MedioClinicUser, int> store, MedioClinicUser user, string password)
        {
            if (user == null)
            {
                return(Task.FromResult(false));
            }

            var userInfo = UserInfoProvider.GetUserInfo(user.UserName);
            var result   = !userInfo.IsExternal && !userInfo.UserIsDomain && !UserInfoProvider.IsUserPasswordDifferent(userInfo, password);

            return(Task.FromResult(result));
        }
Ejemplo n.º 24
0
    protected override async Task <bool> VerifyPasswordAsync(
        IUserPasswordStore <ApplicationUser, string> store,
        ApplicationUser user, string password)
    {
        var hash = await store.GetPasswordHashAsync(user);

        var verifyRes = PasswordHasher.VerifyHashedPassword(hash, password);

        if (verifyRes == PasswordVerificationResult.SuccessRehashNeeded)
        {
            await store.SetPasswordHashAsync(user, PasswordHasher.HashPassword(password));
        }
        return(verifyRes != PasswordVerificationResult.Failed);
    }
        private async Task <IdentityResult> UpdatePasswordInternal(IUserPasswordStore <TUser, TKey> passwordStore, TUser user, string newPassword)
        {
            var result = await PasswordValidator.ValidateAsync(newPassword).WithCurrentCulture();

            if (!result.Succeeded)
            {
                return(result);
            }

            await passwordStore.SetPasswordHashAsync(user, PasswordHasher.HashPassword(newPassword)).WithCurrentCulture();

            await UpdateSecurityStampInternal(user).WithCurrentCulture();

            return(IdentityResult.Success);
        }
Ejemplo n.º 26
0
        private async Task <IdentityResult> UpdatePasswordInternal(IUserPasswordStore <IdentityUser, long> passwordStore,
                                                                   IdentityUser user, string newPassword)
        {
            var result = await PasswordValidator.ValidateAsync(newPassword).ConfigureAwait(false);

            if (!result.Succeeded)
            {
                return(result);
            }
            result = null;
            await passwordStore.SetPasswordHashAsync(user, PasswordHasher.HashPassword(newPassword)).ConfigureAwait(false);

            //await UpdateSecurityStampInternal(user).ConfigureAwait(false);
            return(IdentityResult.Success);
        }
Ejemplo n.º 27
0
        public DiTestController(
            // the Microsoft.AspNetCore.Identity User and Role Manager classes
            RoleManager <IdentityRole> roleManager,
            UserManager <ApplicationUser> userManager,

            IIdentityDatabaseContext <ApplicationUser, IdentityRole, string> identityDatabaseContext,

            // if want to use with SOLID and Interface Segregation Principle, then can just use the specific interface that need

            // these interfaces are all implemented by UserStore
            IUserStore <ApplicationUser> userStore,
            IUserLoginStore <ApplicationUser> userLoginStore,
            IUserRoleStore <ApplicationUser> userRoleStore,
            IUserClaimStore <ApplicationUser> userClaimStore,
            IUserPasswordStore <ApplicationUser> userPasswordStore,
            IUserSecurityStampStore <ApplicationUser> userSecurityStampStore,
            IUserEmailStore <ApplicationUser> userEmailStore,
            IUserLockoutStore <ApplicationUser> userLockoutStore,
            IUserPhoneNumberStore <ApplicationUser> userPhoneNumberStore,
            IUserTwoFactorStore <ApplicationUser> userTwoFactorStore,
            IQueryableUserStore <ApplicationUser> queryableUserStore,

            // these interfaces are all implemented by RoleStore
            IRoleStore <IdentityRole> roleStore,
            IRoleClaimStore <IdentityRole> roleClaimStore,
            IQueryableRoleStore <IdentityRole> queryableRoleStore
            )
        {
            _roleManager = roleManager;
            _userManager = userManager;

            _identityDatabaseContext = identityDatabaseContext;
            _userStore              = userStore;
            _userLoginStore         = userLoginStore;
            _userRoleStore          = userRoleStore;
            _userClaimStore         = userClaimStore;
            _userPasswordStore      = userPasswordStore;
            _userSecurityStampStore = userSecurityStampStore;
            _userEmailStore         = userEmailStore;
            _userLockoutStore       = userLockoutStore;
            _userPhoneNumberStore   = userPhoneNumberStore;
            _userTwoFactorStore     = userTwoFactorStore;
            _queryableUserStore     = queryableUserStore;

            _roleStore          = roleStore;
            _roleClaimStore     = roleClaimStore;
            _queryableRoleStore = queryableRoleStore;
        }
Ejemplo n.º 28
0
        protected async override Task <IdentityResult> UpdatePassword(IUserPasswordStore <ApplicationUser, int> passwordStore, ApplicationUser user, string newPassword)
        {
            try
            {
                user.Password           = newPassword;
                user.PasswordHash       = PasswordHasher.HashPassword(newPassword);
                user.ResetPasswordToken = await GeneratePasswordResetTokenAsync(user.Id);

                await base.UpdateAsync(user);

                return(IdentityResult.Success);
            }
            catch (Exception ex)
            {
                return(IdentityResult.Failed(ex.Message));
            }
        }
        protected override async Task <bool> VerifyPasswordAsync(IUserPasswordStore <CustomUser, int> store, CustomUser user,
                                                                 string password)
        {
            var hash = await store.GetPasswordHashAsync(user).WithCurrentCulture();

            var pwdResult = PasswordHasher.VerifyHashedPassword(hash, password);

            if (pwdResult == PasswordVerificationResult.SuccessRehashNeeded)
            {
                var newHashedPwd = PasswordHasher.HashPassword(password);
                await store.SetPasswordHashAsync(user, newHashedPwd);

                await store.UpdateAsync(user);
            }

            return(pwdResult != PasswordVerificationResult.Failed);
        }
        private async Task <IdentityResult> UpdatePasswordHash(IUserPasswordStore <TUser> passwordStore, TUser user, string newPassword, bool validatePassword = true)
        {
            if (validatePassword)
            {
                var validate = await ValidatePasswordAsync(user, newPassword);

                if (!validate.Succeeded)
                {
                    return(validate);
                }
            }
            var hash = newPassword != null?PasswordHasher.HashPassword(user, newPassword) : null;

            await passwordStore.SetPasswordHashAsync(user, hash, CancellationToken);

            return(IdentityResult.Success);
        }
Ejemplo n.º 31
0
        public AccountController(IUserLoginStore<User, Guid> userLoginStore, IUserPasswordStore<User, Guid> userPasswordStore, IPasswordHasher passwordHasher)
        {
            if (userLoginStore == null)
            {
                throw new ArgumentNullException("userLoginStore");
            }

            if (userPasswordStore == null)
            {
                throw new ArgumentNullException("userPasswordStore");
            }

            if (passwordHasher == null)
            {
                throw new ArgumentNullException("passwordHasher");
            }

            _userLoginStore = userLoginStore;
            _userPasswordStore = userPasswordStore;
            _passwordHasher = passwordHasher;
        }