按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
先前版本
類別庫參考
 HashPasswordForStoringInConfigFile ...
全部折疊/全部展開 全部折疊
本頁僅適用於
Microsoft Visual Studio 2005/.NET Framework 2.0

其他版本也適用於下列軟體:
.NET Framework 類別庫
FormsAuthentication.HashPasswordForStoringInConfigFile 方法

根據指定的密碼和雜湊演算法,產生適用於存放在組態檔中的雜湊密碼。

命名空間: System.Web.Security
組件: System.Web (在 system.web.dll 中)

Visual Basic (宣告)
Public Shared Function HashPasswordForStoringInConfigFile ( _
    password As String, _
    passwordFormat As String _
) As String
Visual Basic (使用方式)
Dim password As String
Dim passwordFormat As String
Dim returnValue As String

returnValue = FormsAuthentication.HashPasswordForStoringInConfigFile(password, passwordFormat)
C#
public static string HashPasswordForStoringInConfigFile (
    string password,
    string passwordFormat
)
C++
public:
static String^ HashPasswordForStoringInConfigFile (
    String^ password, 
    String^ passwordFormat
)
J#
public static String HashPasswordForStoringInConfigFile (
    String password, 
    String passwordFormat
)
JScript
public static function HashPasswordForStoringInConfigFile (
    password : String, 
    passwordFormat : String
) : String

參數

password

要做雜湊演算的密碼。

passwordFormat

要使用的雜湊演算法。passwordFormatString,代表其中一個 FormsAuthPasswordFormat 列舉值。

傳回值

已雜湊的密碼。
例外狀況類型條件

ArgumentNullException

password 為 Null 參照 (即 Visual Basic 中的 Nothing)

-或-

passwordFormat 為 Null 參照 (即 Visual Basic 中的 Nothing)。

ArgumentException

passwordFormat 不是有效的 FormsAuthPasswordFormat 值。

HashPasswordForStoringInConfigFile 方法會建立雜湊的密碼值,可在將表單驗證認證存放於應用程式組態檔中時使用。

存放於應用程式組態檔中的驗證認證是由 Authenticate 方法用來驗證應用程式使用者的密碼。或者,您也可以使用 ASP.NET 成員資格存放使用者認證。如需詳細資訊,請參閱 使用成員資格管理使用者

下列程式碼範例採用了使用者名稱、密碼和雜湊類型,並顯示組態的 credentials 區段,其中包含使用者定義和雜湊的密碼。

Visual Basic
<%@ Page Language="VB" %>
<html>
   <head>
      <script runat="server">
         Sub Cancel_Click(sender As Object, e As EventArgs)
            userName.Text = ""
            password.Text = ""
            repeatPassword.Text = ""
            result.Text = ""
         End Sub
    
         Sub HashPassword_Click(sender As Object, e As EventArgs)
            If Page.IsValid Then
               Dim hashMethod As String = ""

               If md5.Checked Then
                  hashMethod = "MD5"
               Else
                  hashMethod = "SHA1"
               End If
    
               Dim hashedPassword As String = _
                  FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text, hashMethod)
    
               result.Text = "&lt;credentials passwordFormat=""" & hashMethod & _
                 """&gt;<br>" & "  &lt;user name=""" & userName.Text & """ password=""" & _
                 hashedPassword & """ /&gt;<br>" & "&lt;/credentials&gt;"
            Else
               result.Text = "There was an error on the page."
            End If
         End Sub
      </script>
   </head>

   <body>
      <form runat="server">
         <p>This form displays the results of the FormsAuthentication.HashPasswordForStoringInConfigFile
         method.<br>The user name and hashed password can be stored in a &lt;credentials&gt; node
         in the Web.config file.</p>
         <table cellpadding=2>
            <tbody>
               <tr>
                  <td>New User Name:</td>
                  <td><asp:TextBox id="userName" runat="server" /></td>
                  <td><asp:RequiredFieldValidator id="userNameRequiredValidator" 
                        runat="server" ErrorMessage="User name required" 
                        ControlToValidate="userName" /></td>
               </tr>
               <tr>
                  <td>Password: </td>
                  <td><asp:TextBox id="password" runat="server" TextMode="Password" /></td>
                  <td><asp:RequiredFieldValidator id="passwordRequiredValidator" 
                        runat="server" ErrorMessage="Password required" 
                        ControlToValidate="password" /></td>
               </tr>
               <tr>
                  <td>Repeat Password: </td>
                  <td><asp:TextBox id="repeatPassword" runat="server" TextMode="Password" /></td>
                  <td><asp:RequiredFieldValidator id="repeatPasswordRequiredValidator" 
                        runat="server" ErrorMessage="Password confirmation required" 
                        ControlToValidate="repeatPassword" />
                      <asp:CompareValidator id="passwordCompareValidator" runat="server" 
                        ErrorMessage="Password does not match" 
                        ControlToValidate="repeatPassword" 
                        ControlToCompare="password" /></td>
               </tr>
               <tr>
                  <td>Hash function:</td>
                  <td align="middle">
                     <asp:RadioButton id="sha1" runat="server" GroupName="HashType" 
                                      Text="SHA1" />
                     <asp:RadioButton id="md5" runat="server" GroupName="HashType" 
                                      Text="MD5" />
                  </td>
               </tr>
               <tr>
                  <td align="middle" colspan="2">
                    <asp:Button id="hashPassword" onclick="HashPassword_Click" 
                                runat="server" Text="Hash Password" />&nbsp;&nbsp; 
                    <asp:Button id="cancel" onclick="Cancel_Click" runat="server" 
                                Text="Cancel" CausesValidation="false" />
                  </td>
               </tr>
            </tbody>
         </table>

         <pre><asp:Label id="result" runat="server"></asp:Label></pre>
      </form>
   </body>
</html>
C#
<%@ Page Language="C#" %>
<html>
   <head>
      <script runat="server">
         void Cancel_Click(object sender, EventArgs e)
         {
            userName.Text = "";
            password.Text = "";
            repeatPassword.Text = "";
            result.Text = "";
         }
    
         void HashPassword_Click(object sender, EventArgs e)
         {
            if (Page.IsValid)
            {
               string hashMethod = "";

               if (md5.Checked)
               {
                  hashMethod = "MD5";
               }
               else
               {
                  hashMethod = "SHA1";
               }
    
               string hashedPassword =
                  FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text, hashMethod);
    
               result.Text = "&lt;credentials passwordFormat=\"" + hashMethod +"\"&gt;<br>" +
                  "  &lt;user name=\"" + userName.Text + "\" password=\"" +
                  hashedPassword + "\" /&gt;<br>" + "&lt;/credentials&gt;";
            }
            else
            {
               result.Text = "There was an error on the page.";
            }
         }
      </script>
   </head>

   <body>
      <form runat="server">
         <p>This form displays the results of the FormsAuthentication.HashPasswordForStoringInConfigFile
         method.<br>The user name and hashed password can be stored in a &lt;credentials&gt; node
         in the Web.config file.</p>
         <table cellpadding=2>
            <tbody>
               <tr>
                  <td>New User Name:</td>
                  <td><asp:TextBox id="userName" runat="server" /></td>
                  <td><asp:RequiredFieldValidator id="userNameRequiredValidator" 
                        runat="server" ErrorMessage="User name required" 
                        ControlToValidate="userName" /></td>
               </tr>
               <tr>
                  <td>Password: </td>
                  <td><asp:TextBox id="password" runat="server" TextMode="Password" /></td>
                  <td><asp:RequiredFieldValidator id="passwordRequiredValidator" 
                        runat="server" ErrorMessage="Password required" 
                        ControlToValidate="password" /></td>
               </tr>
               <tr>
                  <td>Repeat Password: </td>
                  <td><asp:TextBox id="repeatPassword" runat="server" TextMode="Password" /></td>
                  <td><asp:RequiredFieldValidator id="repeatPasswordRequiredValidator" 
                        runat="server" ErrorMessage="Password confirmation required" 
                        ControlToValidate="repeatPassword" />
                      <asp:CompareValidator id="passwordCompareValidator" runat="server" 
                        ErrorMessage="Password does not match" 
                        ControlToValidate="repeatPassword" 
                        ControlToCompare="password" /></td>
               </tr>
               <tr>
                  <td>Hash function:</td>
                  <td align="middle">
                     <asp:RadioButton id="sha1" runat="server" GroupName="HashType" 
                                      Text="SHA1" />
                     <asp:RadioButton id="md5" runat="server" GroupName="HashType" 
                                      Text="MD5" />
                  </td>
               </tr>
               <tr>
                  <td align="middle" colspan="2">
                    <asp:Button id="hashPassword" onclick="HashPassword_Click" 
                                runat="server" Text="Hash Password" />&nbsp;&nbsp; 
                    <asp:Button id="cancel" onclick="Cancel_Click" runat="server" 
                                Text="Cancel" CausesValidation="false" />
                  </td>
               </tr>
            </tbody>
         </table>

         <pre><asp:Label id="result" runat="server"></asp:Label></pre>
      </form>
   </body>
</html>

Windows 98、 Windows 2000 SP4、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

.NET Framework

支援版本:2.0、1.1、1.0
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2009 Microsoft Corporation. 著作權所有,並保留一切權利。 使用規定 | 商標 | 隱私權聲明
Page view tracker