【问题标题】:Encrypt/Decrypt password usage in a custom MembershipProvider?在自定义 MembershipProvider 中加密/解密密码使用?
【发布时间】:2010-11-06 11:10:17
【问题描述】:

我实现了一个自定义成员资格提供程序。 我还实现了我的自定义 AES 加密/解密,覆盖了 MembershipProvider 的抽象 EncryptPassword/DecryptPassword。

但是,当我触发 ValidateUser 时,密码并没有自动转换,是我遗漏了什么吗?

它应该被自动调用还是我必须从我的代码中调用这个方法?

【问题讨论】:

  • 我不熟悉 MembershipProvider,但存储可解密密码几乎总是不正确的。您应该使用像 SHA256 这样的单向哈希。如果您必须使用该密码对另一个明文身份验证系统进行身份验证,则例外情况。
  • 我将加密的密码存储在 varbinary 中。当用户登录时,它们会被解密。

标签: asp.net encryption cryptography passwords membership-provider


【解决方案1】:

您需要自己调用加密例程。

【讨论】:

  • 所以我可以将 DecryptPassword 函数声明为新函数吗?即 public new string DecriptPassword(byte[] password) 而不是基类模式 public byte[] DecryptPassword(byte[] password)?
  • 如果您正在实施会员提供程序,您应该能够只覆盖这些功能,而不是使用“新”。然后,无论您需要在何处将密码转换为明文/从明文转换,例如在 ValidateUser 中,您都可以调用相应的函数。它们出现在界面中的原因是为您的提供者的消费者提供加密/解密密码的一致方式,而不是让他们对您使用的加密算法一无所知。
【解决方案2】:

您需要包含对您的加密/解密方法的调用,就像应该做的那样:

public override bool ValidateUser(string username, string password)
{
    string password=query to get the password from the db/users_list;

    return (CheckPassword(password, storedPassword));
}

private bool CheckPassword(string password, string dbpassword)
{
    string pwd1 = password;
    string pwd2 = dbpassword;

    pwd2 = UEncodePassword(dbpassword);

    if (pwd1 == pwd2) return true;

    return false;
}

private string UEncodePassword(string encodedPassword)
{
    string password = encodedPassword;
    password = Encoding.Unicode.GetString(DecryptPassword(Convert.FromBase64String(encodedPassword)));

    return password;
}

【讨论】:

  • 您应该无法解码密码。如果可以,那么其他人也可以,您不妨以明文形式存储它。非常糟糕的做法。
  • 我刚试着回答别人的问题。不是因为有人需要做一些你认为不好的做法,所以我的回答必须被认为是错误的。你应该回答这个问题并告诉他他想做什么是不好的做法!
猜你喜欢
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多