C#中密码重置流的最佳实践

作者:微信公众号:【架构师老卢】
9-17 19:8
97

密码重置功能是任何处理用户帐户的 Web 应用程序的关键部分。实施安全且用户友好的密码重置流程有助于保护用户的帐户免受未经授权的访问。以下是一些要遵循的最佳实践以及 C# 中的示例。

1. 使用安全的随机代币

错误的方式:

生成 4 位或 6 位代码等简单令牌非常不安全,因为它们很容易被猜到或暴力破解。

// Using a simple 4-digit code is not secure  
public static string GenerateInsecureToken()  
{  
    Random random = new Random();  
    int token = random.Next(1000, 9999); // Generates a 4-digit token  
    return token.ToString();  
}

正确的方式:

确保令牌是从安全的随机源生成的,并且足够长(至少 64 个字符)。使用 Base64 编码使令牌既安全又易于管理。

using System.Security.Cryptography;  
  
public static string GenerateSecureToken(int length = 64)  
{  
    using (var rng = new RNGCryptoServiceProvider())  
    {  
        var tokenData = new byte[length];  
        rng.GetBytes(tokenData);  
        return Convert.ToBase64String(tokenData);  
    }  
}

2. 数据库中的哈希令牌

错误的方式:

如果数据库遭到入侵,将令牌以纯文本形式存储在数据库中可能会导致严重的安全问题。

// Storing the token directly in the database  
string token = GenerateSecureToken();  
// Save `token` to the database

正确的方式:

在数据库中仅存储经过哈希处理的令牌版本,以防止数据库被盗。

using System.Security.Cryptography;  
using System.Text;  
  
public static string HashToken(string token)  
{  
    using (var sha256 = SHA256.Create())  
    {  
        var tokenBytes = Encoding.UTF8.GetBytes(token);  
        var hashedBytes = sha256.ComputeHash(tokenBytes);  
        return Convert.ToBase64String(hashedBytes);  
    }  
}  
  
// Storing the hashed token  
string token = GenerateSecureToken();  
string hashedToken = HashToken(token);  
// Save `hashedToken` to the database

3. 过期令牌

错误的方式:

允许令牌无限期保持有效可能会使应用程序面临长期风险。

// Token with no expiration  
PasswordResetToken resetToken = new PasswordResetToken  
{  
    Token = HashToken(GenerateSecureToken())  
};  
  
// Save `resetToken` to the database without an expiration

正确的方式:

为令牌设置合理的过期时间,以限制攻击的机会窗口。

public class PasswordResetToken  
{  
    public string Token { get; set; }  
    public DateTime Expiration { get; set; }  
}  
  
// Generating a token with an expiration time  
PasswordResetToken resetToken = new PasswordResetToken  
{  
    Token = HashToken(GenerateSecureToken()),  
    Expiration = DateTime.UtcNow.AddHours(1) // Token expires in 1 hour  
};  
  
// Save `resetToken` to the database

4. 实施速率限制

错误的方式:

允许无限制的密码重置请求可能会使应用程序遭受滥用和拒绝服务攻击。

// No rate limiting in place  
public void RequestPasswordReset(string userEmail)  
{  
    // Generate and send reset token  
}

正确的方式:

为防止滥用,请限制在给定时间范围内可以发出的密码重置请求的数量。

public class RateLimiter
{
    private readonly Dictionary<string, DateTime> _requestLog = new Dictionary<string, DateTime>();
    private readonly TimeSpan _timeFrame = TimeSpan.FromMinutes(15);
    private readonly int _maxRequests = 5;

    public bool IsAllowed(string userEmail)
    {
        if (_requestLog.TryGetValue(userEmail, out DateTime lastRequestTime))
        {
            if (lastRequestTime > DateTime.UtcNow.Subtract(_timeFrame))
            {
                return false; // Limit exceeded
            }
        }

        _requestLog[userEmail] = DateTime.UtcNow;
        return true;
    }
}

// Check if the request is allowed before generating a token
RateLimiter rateLimiter = new RateLimiter();
if (rateLimiter.IsAllowed(userEmail))
{
    // Generate and send reset token
}
else
{
    // Inform the user about rate limiting
}

5. 安全令牌交付

错误的方式:

通过不安全的通道发送令牌或在 URL 中公开令牌可能会导致拦截和滥用。

// Sending token in plain text via email  
public void SendPasswordResetEmail(string email, string token)  
{  
    string resetLink = $"https://example.com/reset-password?token={token}";  
  
    // Code to send email  
    // Sending email without secure protocols  
}

正确的方式:

通过安全的电子邮件渠道发送令牌,并考虑使用其他身份验证层,例如双因素身份验证。

public void SendPasswordResetEmail(string email, string token)  
{  
    string resetLink = $"https://example.com/reset-password?token={token}";  
    // Code to send email  
    // Ensure the email sending service uses secure protocols (e.g., TLS)  
}  
  
// Generate token and send email  
string token = GenerateSecureToken();  
SendPasswordResetEmail(userEmail, token);

实施安全的密码重置流程对于保护用户帐户至关重要。通过生成安全令牌、在数据库中对令牌进行哈希处理、适当地过期令牌、实施速率限制以及安全地交付令牌,您可以显著提高密码重置过程的安全性。将这些做法合并到 .NET 应用程序中,以确保可靠且安全的密码重置机制。

相关留言评论
昵称:
邮箱:
阅读排行