1

適切にハッシュ化されたパスワードを持たない既存のアカウントに対して、Web アプリケーションのユーザー名とログインをすばやく生成するためのクイック コンソール アプリを作成しました。私の 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;

これらの 2 つの方法では、異なる結果が得られます。FormsAuthentication と同じ結果を得る必要があります。私はセキュリティの専門家ではありませんが、背景がわずかしかなく、文字セットの知識はさらに劣っています。どんな助けにも感謝します。

4

5 に答える 5

3

これは、フォーマットを一致させる方法の良い説明です。それが役に立てば幸い。

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

違いは、フォーム認証がそれを16進数の文字列にハッシュすることだと思います。

于 2009-08-06T19:58:32.697 に答える
3

それは私のために働くようです。

  • System.Web への参照を追加しました
  • System.Web.Security を使用して追加しました

次に、コードスニペットを使用しました:

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

結果を得るために。これはコンソール アプリケーションにありました。

于 2009-08-06T20:03:43.273 に答える
2

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();
于 2009-08-18T14:20:25.393 に答える
2

興味があれば、そのメソッドで使用されるコードは次のとおりです。

return MachineKeySection.ByteArrayToHexString(
    SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(password)),
    0
);
于 2009-08-06T20:03:37.283 に答える
0

System.Web 参照をコンソール アプリに追加して使用することができました

string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1");
于 2009-08-06T20:04:57.703 に答える