asp.net - AspNet 成员身份加密密码生成
问题描述
在我们开始之前,我想明确表示我知道这个问题有多糟糕。这是一个可怕的情况,但我受到一些非常奇怪的规范的限制。
我知道你不仅不应该尝试为这类东西编写自己的包装器,而且微软已经明确表示不应该使用它。所以在你开始写回复说“你为什么要这样做”之前,请试着理解我已经和我的上级进行了这些对话,但是推动新功能和缺乏时间意味着尽管它是残酷的;尽管如此 - 我在这里。
我们有一个 ASP Net 会员数据库,早在我在这家公司工作之前就开始了,现在拥有大约 4 万用户。我们在 .Net 3.5 中有一个平台,可以让用户登录。
我目前的工作是在 .Net Core 2.1 中编写一个 API,其中一部分是允许用户创建和更新,但问题在于 - 从成员身份迁移到身份不是一种选择,所以我被告知要创建一个包装器用于成员资格数据库中的存储过程。
这主要是成功的,唯一的问题是;这个问题的主题。通过我创建用户aspnet_Membership_CreateUser
需要以可以在我们的平台上成功验证的方式提交数据。
我最初关注了这篇文章,但发现它是为 PasswordFormat 1 - Hashed 设计的;然后我发现我们的用户群使用 PasswordFormat 2 - Encrypted,因此我创建的用户不会验证。
代码看起来像这样
public bool CreateUser(string userName, string password, string email, string securityQuestion, string securityAnswer, bool isApproved)
{
bool success = false;
//Just so I can attempt to login afterwards
password = "Hello World!";
//Our password and password salt need to be base64 encoded before we can save them to the DB
string salt = Guid.NewGuid().ToString();
string encryptedSalt = salt.Base64Encode();
//Concatenate our salt and password
IEnumerable<byte> saltedpass = salt.GetBytes(Encoding.UTF8).Concat(password.GetBytes(Encoding.UTF8));
//Use SHA1 to hash more - equivilant to the HASHBYTES('SHA1' T-SQL
byte[] sha1HashedPass = PasswordHelper.HashBytes(_validationMethod, saltedpass.ToArray(), _validationKey);
string hashedPass = sha1HashedPass.ToBase64String();
int errorCode = MembershipCreateUser(_applicationName, userName, hashedPass, encryptedSalt, email, securityQuestion, securityAnswer, isApproved);
if (errorCode == 0)
{
success = true;
}
return success;
}
值得注意的_validationKey
是,使用该数据库的应用程序之间共享的机器密钥,我将其传递给 SHA1 机制。
因此,除了故意和可悲的不良安全做法外;C# 中有没有办法以这种方式生成加密(非散列)密码和盐?
解决方案
感谢您的评论 - 谢天谢地,我们能够在我们的平台中支持散列密码;问题出在我的代码上,而不是 ASP 会员。
如前所述,我正在使用最初用 T-SQL 编写的帖子并尝试构建它的 C# 实现。我对这段代码的实现不正确,因此我生成的密码和盐无法通过 ASP Net Membership 进行验证,这在我的原始帖子中并不明显,因为我混淆了 SHA1 散列我的数据的方法。
//Using hard coded just for example
string username = "joeborder";
string password = "Hello World!";
string salt = "TastySalt";
Encoding encoder = Encoding.Unicode; //Encoding was also incorrect
//Our password and password salt need to be base64 encoded before we can save them to the DB
string encryptedSalt = salt.Base64Encode();
//Concatenate our salt and password
IEnumerable<byte> saltedpass = salt.GetBytes(encoder).Concat(password.GetBytes(encoder));
//Use SHA1 to hash more - equivilant to the HASHBYTES('SHA1') T-SQL
var SHA1Hasher = new SHA1CryptoServiceProvider(); //Originally I was using HMACSHA1 which was producing a different output
byte[] sha1HashedPass = SHA1Hasher.ComputerHash(saltedpass.ToArray());
string hashedPass = sha1HashedPass.ToBase64String();
/*
EXEC aspnet_Membership_CreateUser
@ApplicationName = "MyApp",
@UserName = username,
@Password = hashedPass,
@PasswordSalt = encryptedSalt,
...Etc
*/
然后在我们的 .Net 3.5 应用程序中,以下代码将起作用
string username = "joeborder";
string password = "Hello World!";
if (Membership.ValidateUser(username, password))
{
Console.WriteLine("You've gotta be kidding me thats a clutch");
}
推荐阅读
- ios - 我无法加载在插座中设置的视图
- asp.net-core - 如何从 .NET Core 中的单独文件嵌套 Avro 架构
- ios - 通用链接未使用子文件夹名称打开,仅在域名时有效
- python - 如何过滤出 Pandas DataFrame 中包含列表列中特定子序列的行?
- c++ - 递减/非递增向量的下限和上限
- netbeans - NetBeans 12.3 - 代码完成和代码建议非常慢
- rest - Power BI REST API ExportToFileInGroup 不工作
- javascript - 空的时如何将蓄能器散布在减速器中?
- ssas-tabular - SSAS 表格模型使用遥测
- android - 每次在Android中更改选择时如何重置函数调用