WebMatrix WebSecurity PasswordSalt

时间:2011-02-25 12:53:09

标签: c# asp.net-mvc passwords webmatrix salt

我正在使用WebMatrix,并建立了一个基于“StarterSite”的网站。在这个入门网站中,您将获得一个很好的基本布局 - 包括注册,登录,忘记密码页面等......

我注意到在数据库中“webpages_Membership”表有一个名为“PasswordSalt”的列。创建一些新用户帐户后,此列始终为空。所以我假设没有使用密码盐(甚至不是默认密码)。

显然这不是最好的做法,但我似乎无法找到任何告诉我如何设置或管理密码盐的文档。

如何使用WebSecurity Helper设置密码盐?

2 个答案:

答案 0 :(得分:36)

上述答案给人的印象是,使用WebSecurity SimpleMembershipProvider时没有应用腌制。

事实并非如此。实际上,未使用数据库salt字段,但这并不表示在对密码进行散列时不会生成 salt

WebSecurity s SimpleMembershipProvider中使用了PBKDF2算法,随机盐由StaticRandomNumberGenerator生成并存储在带有哈希的密码字段中:

byte[] outputBytes = new byte[1 + SALT_SIZE + PBKDF2_SUBKEY_LENGTH];
Buffer.BlockCopy(salt, 0, outputBytes, 1, SALT_SIZE); 
Buffer.BlockCopy(subkey, 0, outputBytes, 1 + SALT_SIZE, PBKDF2_SUBKEY_LENGTH);
return Convert.ToBase64String(outputBytes);

答案 1 :(得分:4)

从WebMatrix / ASP.NET网页的RTM版本开始,盐功能/列未使用。

如果打开网页源代码,您会看到数据库类似于

等引用
INSERT INTO [" + MembershipTableName + "] (UserId, [Password], PasswordSalt

...

VALUES (uid, hashedPassword,String.Empty /* salt column is unused */

缩短了重点

有明确的方法来覆盖和实现此行为,首先是:

  • 覆盖System.WebData.SimpleMembershipProvider.CreateAccount()

  • 使用System.WebData.SimpleMembershipProvider.CreateAccountWithPasswordSalt()进行扩展

除非你要求,否则不会详细说明,因为你使用WebMatrix和模板表明你可能不想为这个项目重写你自己的C#/ ASP代码。