コード例 #1
0
        public async Task <IdentityResult> ChangePasswordAsync(IdentityUser user, string currentPassword, string newPassword)
        {
            var verifyResult = _hasher.VerifyHashedPassword(user.Id, user.PasswordHash, currentPassword);

            if (verifyResult != Microsoft.AspNetCore.Identity.PasswordVerificationResult.Success)
            {
                return(new IdentityResult()
                {
                    Succeeded = false,
                    Errors = new List <string>()
                    {
                        "Incorrect password supplied"
                    }
                });
            }

            newPassword = newPassword.Trim();

            if (string.IsNullOrEmpty(newPassword) || newPassword.Length < 7)
            {
                return(new IdentityResult()
                {
                    Errors = new List <string> {
                        "Password must be at least 7 characters"
                    },
                    Succeeded = false
                });
            }


            using (var db = new SqlDbContext(_options))
            {
                try
                {
                    var dbUser = db.IdentityUser.FirstOrDefault(tbl => tbl.Id == user.Id);

                    if (dbUser == null)
                    {
                        user.PasswordHash = _hasher.HashPassword(user.Id, newPassword);

                        db.Update(user);
                    }
                    await db.SaveChangesAsync();
                }
                catch (Exception exc)
                {
                }

                return(new IdentityResult()
                {
                    Succeeded = true
                });
            }
        }
コード例 #2
0
        public bool IsPasswordValid(string hashedPassword, string password)
        {
            try
            {
                var verified = _passwordHasher.VerifyHashedPassword(null, hashedPassword, password);
                return(verified == Microsoft.AspNetCore.Identity.PasswordVerificationResult.Success);
            }

            // if hashed password is not a base64 encoded string
            catch (System.FormatException)
            {
                return(false);
            }
        }