62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using MudBlazor;
|
|
|
|
namespace RobotNet.IdentityServer.Services;
|
|
|
|
public class PasswordStrengthService
|
|
{
|
|
/// <summary>
|
|
/// Đánh giá độ mạnh của mật khẩu (thang điểm 0-100)
|
|
/// </summary>
|
|
/// <param name="password">Mật khẩu cần đánh giá</param>
|
|
/// <returns>Điểm đánh giá từ 0-100</returns>
|
|
public int EvaluatePasswordStrength(string password)
|
|
{
|
|
if (string.IsNullOrEmpty(password))
|
|
return 0;
|
|
|
|
int strength = 0;
|
|
|
|
// Đánh giá dựa trên độ dài
|
|
if (password.Length >= 1) strength += 5;
|
|
if (password.Length >= 3) strength += 5;
|
|
if (password.Length >= 6) strength += 10;
|
|
if (password.Length >= 8) strength += 10;
|
|
if (password.Length >= 10) strength += 10;
|
|
|
|
// Đánh giá dựa trên độ phức tạp
|
|
if (password.Any(char.IsUpper)) strength += 15;
|
|
if (password.Any(char.IsLower)) strength += 15;
|
|
if (password.Any(char.IsDigit)) strength += 15;
|
|
if (password.Any(c => !char.IsLetterOrDigit(c))) strength += 15;
|
|
|
|
return System.Math.Min(strength, 100);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy màu tương ứng với độ mạnh của mật khẩu
|
|
/// </summary>
|
|
/// <param name="strength">Điểm đánh giá độ mạnh (0-100)</param>
|
|
/// <returns>Color tương ứng</returns>
|
|
public Color GetStrengthColor(int strength)
|
|
{
|
|
if (strength < 30) return Color.Error;
|
|
if (strength < 60) return Color.Warning;
|
|
if (strength < 80) return Color.Info;
|
|
return Color.Success;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy mô tả tương ứng với độ mạnh của mật khẩu
|
|
/// </summary>
|
|
/// <param name="strength">Điểm đánh giá độ mạnh (0-100)</param>
|
|
/// <returns>Mô tả dạng văn bản</returns>
|
|
public string GetStrengthDescription(int strength)
|
|
{
|
|
if (strength == 0) return "Chưa nhập mật khẩu";
|
|
if (strength < 30) return "Mật khẩu yếu";
|
|
if (strength < 60) return "Mật khẩu trung bình";
|
|
if (strength < 80) return "Mật khẩu tốt";
|
|
return "Mật khẩu mạnh";
|
|
}
|
|
}
|