Forms 身份验证实用工具

更新:2007 年 11 月

要管理 Forms 身份验证,可以使用 FormsAuthentication 类的静态方法。下表列出了这些方法。

方法

说明

Authenticate

给定提供的凭据,尝试验证已配置凭据存储区的凭据。

Decrypt

在给定从 HTTP Cookie 获得的加密身份验证票证的情况下,返回 FormsAuthenticationTicket 类的一个实例。

Encrypt

在给定 FormsAuthenticationTicket 的情况下,生成包含适合用在 HTTP Cookie 中的加密身份验证票证的字符串。

GetAuthCookie

检索加密的身份验证 Cookie(作为一个 HttpCookie 实例)。此 Cookie 不添加到 Cookies 集合中。

GetRedirectUrl

为导致重定向到登录页的请求返回重定向 URL。

HashPasswordForStoringInConfigFile

在给定密码和标识哈希类型的字符串的情况下,生成适合存储在配置文件中的哈希密码。

Initialize

通过读取配置设置并获取当前应用程序的 Cookie 值和加密值,来初始化 FormsAuthentication 类。

RedirectFromLoginPage

将已通过身份验证的用户重定向到最初请求的 URL。

RenewTicketIfOld

更新 FormsAuthenticationTicket 上的可调过期。

SetAuthCookie

创建身份验证票并将它附加到传出响应的 Cookie 集合。

SignOut

通过将身份验证 Cookie 或 URL 文本设置为空值来移除身份验证票证。这将移除持久性的 Cookie 和会话 Cookie。

874sbx60.alert_caution(zh-cn,VS.90).gif重要说明:
虽然 SignOut 方法会从通过身份验证的浏览器会话中清除票证,但您的应用程序仍然易于受到来自已“探查”到身份验证票证的有害源的重播攻击。有关利用 Forms 身份验证降低受到重播攻击的可能性的信息,请参见 SignOut

下表列出了有助于管理 Forms 身份验证票的属性。

属性

说明

FormsCookieName

获取当前应用程序的 Cookie 名称。

FormsCookiePath

获取当前应用程序的 Cookie 路径。

CookiesSupported

获取一个值,该值指示应用程序是否已配置为支持无 Cookie Forms 身份验证。

CookieMode

获取一个值,该值指示是否已对应用程序配置了无 Cookie Forms 身份验证。

CookieDomain

获取 Forms 身份验证 Cookie 的域的值。

DefaultUrl

获取在未指定重定向 URL 时 Forms 身份验证将重定向到的 URL。

LoginUrl

获取 Forms 身份验证将重定向到的登录页的 URL。

RequireSSL

获取一个值,该值指示是否必须使用安全套接字层 (SSL) 来传输 Cookie。

SlidingExpiration

获取一个值,该值指示是否启用弹性过期时间。

EnableCrossAppRedirects

获取一个值,该值指示当 Forms 身份验证票证未存储在 Cookie 中时,是否可以将通过身份验证的用户重定向到其他 Web 应用程序中的 URL。

可以使用 FormsAuthentication 类的方法来自定义 Forms 身份验证的工作方式。还可以在登录页处理程序中使用它们,这样便无需对重定向进行显式编码。下面的代码示例演示一个对用户进行身份验证并将其重定向到所请求页的 ASP.NET 网页。

<html>
<head>
<script language="VB" runat=server>
    Sub SubmitBtn_Click(Source As Object, e As EventArgs)
        ' Try to authenticate credentials supplied by user.
        If FormsAuthentication.Authenticate _
                (UserName.Value, UserPassword.Value) Then
            Dim ticket As New FormsAuthenticationTicket _
                (UserName.Value, False, 5000)
            FormsAuthentication.RedirectFromLoginPage _
                (UserName.Value, Persist.Checked)
        End If
    End Sub
</script>
</head>

<body>
<form method=post runat=server>
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" id="UserName" runat=server/>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" id="UserPassword" runat=server/>
            </td>
        </tr>
    </table>
    <input type="checkbox" id="Persist" runat=server/>
    <!-- Use persistent cookie -->
    <br>
    <input type="submit" OnServerClick="SubmitBtn_Click" runat=server/>
</form>
</body>
</html>
<html>
<head>
<script language="C#" runat=server>
    void SubmitBtn_Click(Object Source, EventArgs e)
    {
        // Try to authenticate credentials supplied by user.
        if (FormsAuthentication.Authenticate(UserName.Value, 
                UserPassword.Value))
        {
            FormsAuthenticationTicket ticket = new 
                FormsAuthenticationTicket(UserName.Value, false, 5000);
                  
            FormsAuthentication.RedirectFromLoginPage(UserName.Value,
                Persist.Checked);
        }
    }
</script>
</head>

<body>

<form method=post runat=server>
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" id="UserName" runat=server/></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" id="UserPassword" runat=server/>
            </td>
        </tr>
    </table>
    <input type="checkbox" id="Persist" runat=server/>
    <!-- Use persistent cookie. -->
    <br>
    <input type="submit" OnServerClick="SubmitBtn_Click" runat=server/>
</form>
</body>
</html>

需要对 HTTP Cookie 属性进行精确控制的应用程序可以构造票证,并执行自定义代码中的重定向。在这些情况下,应使用 FormsAuthentication 类的加密方法来加密身份验证票证。

请参见

参考

FormsAuthentication

FormsAuthenticationTicket

HttpCookie

其他资源

ASP.NET Web 应用程序安全性

Forms 身份验证提供程序