对于非 Web 应用程序,什么会给我带来与 FormsAuthentication 哈希相同的结果?

发布于 2024-07-29 21:45:59 字数 785 浏览 3 评论 0原文

我编写了一个快速控制台应用程序,可以为没有正确散列密码的现有帐户快速生成 Web 应用程序的用户名和登录名。 在我的网络应用程序中,我使用 FormsAuthentication,如下所示:

string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1");

我尝试在控制台应用程序中使用 FormsAuthentication,但它无法解析 FormsAuthentication 或导入。 我收到的警告询问我是否缺少程序集。 我尝试使用以下方法来获得与之前相同的结果:

SHA1 sha1 = new SHA1CryptoServiceProvider();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytesHashedPwd = sha1.ComputeHash(encoding.GetBytes(saltAndPwd));
string tmpString =  encoding.GetString(byteshashedPwd);
string hashedPwd = String.Concat(str, salt);
return hashedPwd;

这两种方法给了我不同的结果。 我需要获得与 FormsAuthentication 相同的结果。 我不是一个没有什么浮夸背景的安全专家,我的字符集知识更糟糕。 我感谢任何帮助。

I have written a quick console app to quickly go and generate usernames and logins for my web application for existing accounts that did not have properly hashed passwords. In my web application I am using FormsAuthentication like so:

string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1");

I tried to use FormsAuthentication in the console app but it cannot resolve the FormsAuthentication nor the imports. The warning I get asks if I am missing an assembly. I tried to use the following to give me the same results as the previous:

SHA1 sha1 = new SHA1CryptoServiceProvider();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytesHashedPwd = sha1.ComputeHash(encoding.GetBytes(saltAndPwd));
string tmpString =  encoding.GetString(byteshashedPwd);
string hashedPwd = String.Concat(str, salt);
return hashedPwd;

These two methods are giving me different results. I need to get the same result as FormsAuthentication. I am no security expert with a tiny vauge background and my character set knowledge is even worse. I apprecaite any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鸢与 2024-08-05 21:46:00

这似乎对我有用。

  • 我添加了对 System.Web 的引用,
  • 我使用 System.Web.Security 添加了

然后我使用了您的代码片段:

string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1");

来获取结果。 这是在控制台应用程序中。

It seems to work for me.

  • I added a reference to System.Web
  • I added using System.Web.Security

Then I used your code snippet:

string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1");

To get the result. This was in a console application.

乖乖哒 2024-08-05 21:46:00

如果您好奇,该方法使用的代码是:

return MachineKeySection.ByteArrayToHexString(
    SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(password)),
    0
);

If you're curious, the code used by that method is:

return MachineKeySection.ByteArrayToHexString(
    SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(password)),
    0
);
梦明 2024-08-05 21:46:00

这是在 C# 中避免 System.Web.dll 依赖性的另一个解决方案:

public static string SHA1(this string stringToHash)
{
    // SHA1 is 160bit
    SHA1 sha = new SHA1Managed();
    byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(stringToHash));

    StringBuilder stringBuilder = new StringBuilder();
    foreach (byte b in hash)
    {
        stringBuilder.AppendFormat("{0:x2}", b);
    }

    return stringBuilder.ToString().ToUpper(); // HashPasswordForStoringInConfigFile uses uppercase hex
}

您可以像这样使用它:

string salt = "abcdef"; // swap this with a random string generator
string password = string.Format("{0}{1}", "password", salt).SHA1();

Here's another solution to avoid the System.Web.dll dependency, in C#:

public static string SHA1(this string stringToHash)
{
    // SHA1 is 160bit
    SHA1 sha = new SHA1Managed();
    byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(stringToHash));

    StringBuilder stringBuilder = new StringBuilder();
    foreach (byte b in hash)
    {
        stringBuilder.AppendFormat("{0:x2}", b);
    }

    return stringBuilder.ToString().ToUpper(); // HashPasswordForStoringInConfigFile uses uppercase hex
}

You'd use it like this:

string salt = "abcdef"; // swap this with a random string generator
string password = string.Format("{0}{1}", "password", salt).SHA1();
落日海湾 2024-08-05 21:46:00

我能够将 System.Web 引用添加到控制台应用程序并使用

string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1");

I was able to add the System.Web reference to a console app and use

string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1");
热鲨 2024-08-05 21:45:59

这里很好地解释了如何匹配格式。 希望能帮助到你。

http://www.stardeveloper.com/articles/display。 html?article=2003062001&page=1

我相信区别在于表单身份验证将其哈希为十六进制字符串。

Here is a good explanation of how to match the format. Hope it helps.

http://www.stardeveloper.com/articles/display.html?article=2003062001&page=1

I believe the difference is the forms authentication hashes it to a hexadecimal string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文