FormsAuthentication 类

定义

为 Web 应用程序管理 Forms 身份验证服务。 此类不能被继承。

public ref class FormsAuthentication sealed
public sealed class FormsAuthentication
type FormsAuthentication = class
Public NotInheritable Class FormsAuthentication
继承
FormsAuthentication

示例

下面的代码示例演示 ASP.NET 应用程序的Web.config文件,该应用程序使用 ASP.NET 成员资格提供程序进行表单身份验证,并要求对所有用户进行身份验证。

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString="Data Source=MySqlServer;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
  </connectionStrings>
  <system.web>
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
      <providers>
        <add name="SqlProvider"
          type="System.Web.Security.SqlMembershipProvider"
          connectionStringName="SqlServices"
          enablePasswordRetrieval="false"
          enablePasswordReset="true"
          requiresQuestionAndAnswer="true"
          passwordFormat="Hashed"
          applicationName="/" />
      </providers>
    </membership>
  </system.web>
</configuration>

下面的代码示例演示使用表单身份验证和 ASP.NET 成员身份的 ASP.NET 应用程序的登录页。

重要

此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Login_OnClick(object sender, EventArgs args)
{
   if (Membership.ValidateUser(UsernameTextbox.Text, PasswordTextbox.Text))
      FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text, NotPublicCheckBox.Checked);
   else
     Msg.Text = "Login failed. Please check your user name and password and try again.";
}


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Login</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Login</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  Username: <asp:Textbox id="UsernameTextbox" runat="server" /><br />
  Password: <asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /><br />
 
  <asp:Button id="LoginButton" Text="Login" OnClick="Login_OnClick" runat="server" />
  <asp:CheckBox id="NotPublicCheckBox" runat="server" /> 
  Check here if this is <span style="text-decoration:underline">not</span> a public computer.

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Login_OnClick(sender As Object, args As  EventArgs)

   If (Membership.ValidateUser(UsernameTextbox.Text, PasswordTextbox.Text)) Then
      FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text, NotPublicCheckBox.Checked)
   Else
     Msg.Text = "Login failed. Please check your user name and password and try again."
   End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Login</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Login</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  Username: <asp:Textbox id="UsernameTextbox" runat="server" /><br />
  Password: <asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /><br />
 
  <asp:Button id="LoginButton" Text="Login" OnClick="Login_OnClick" runat="server" />
  <asp:CheckBox id="NotPublicCheckBox" runat="server" /> 
  Check here if this is <span style="text-decoration:underline">not</span> a public computer.

</form>

</body>
</html>

注解

表单身份验证为不需要Windows 身份验证的 Web 应用程序启用用户和密码验证。 使用表单身份验证时,用户信息将存储在外部数据源(如 Membership 数据库)中,或存储在应用程序的配置文件中。 对用户进行身份验证后,表单身份验证会在 Cookie 或 URL 中维护身份验证票证,以便经过身份验证的用户无需为每个请求提供凭据。

通过将身份验证配置元素的 属性设置为 modeForms启用表单身份验证。 通过使用 授权 配置元素拒绝任何未知用户的请求,可以要求对应用程序的所有请求都包含有效的用户身份验证票证,如以下示例所示。

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="login.aspx" />
  </authentication>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>

在前面的示例中,对属于应用程序的 ASP.NET 页的任何请求都需要表单身份验证提供的有效用户名。 如果不存在用户名,则会将请求重定向到配置的 LoginUrl

FormsAuthentication 提供对方法和属性的访问,这些方法和属性可用于对用户进行身份验证的应用程序。 方法 RedirectToLoginPage 将浏览器重定向到配置的 LoginUrl ,以便用户登录到应用程序。 方法 RedirectFromLoginPage 将经过身份验证的用户重定向回请求的原始受保护 URL 或 DefaultUrl。 还有一些方法可用于管理表单身份验证票证(如果需要)。

构造函数

FormsAuthentication()

初始化 FormsAuthentication 类的新实例。

属性

CookieDomain

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

CookieMode

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

CookieSameSite

获取或设置 cookie 的 SameSite 属性的值。

CookiesSupported

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

DefaultUrl

获取在没有指定重定向 URL 时 FormsAuthentication 类将重定向到的 URL。

EnableCrossAppRedirects

获取一个值,该值指示是否可以将经过身份验证的用户重定向到其他 Web 应用程序中的 URL。

FormsCookieName

获取用于存储 Forms 身份验证票证的 Cookie 名称。

FormsCookiePath

获取 Forms 身份验证 Cookie 的路径。

IsEnabled

获取一个值,该值指示是否启用了 Forms 身份验证。

LoginUrl

获取 FormsAuthentication 类将重定向到的登录页的 URL。

RequireSSL

获取一个值,指示 Forms 身份验证 Cookie 是否需要 SSL 以返回到服务器。

SlidingExpiration

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

TicketCompatibilityMode

获取一个指示是否使用于票证到期日期的“使用协调世界时” (UTC) 或本地时间的值。

Timeout

获取身份验证票证到期前的时间量。

方法

Authenticate(String, String)
已过时.

对照存储在应用程序配置文件中的凭据来验证用户名和密码。

Decrypt(String)

创建一个 FormsAuthenticationTicket 对象,此对象将根据传递给该方法的加密的 Forms 身份验证票证而定。

EnableFormsAuthentication(NameValueCollection)

启动窗体验证。

Encrypt(FormsAuthenticationTicket)

创建一个字符串,其中包含适用于 HTTP Cookie 的加密的 Forms 身份验证票证。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetAuthCookie(String, Boolean)

为给定的用户名创建身份验证 Cookie。 这不会将 Cookie 设置为传出响应的一部分,因此应用程序对如何发出该 Cookie 有更多的控制权限。

GetAuthCookie(String, Boolean, String)

为给定的用户名创建身份验证 Cookie。 这不会将 Cookie 设置为传出响应的一部分。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetRedirectUrl(String, Boolean)

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

GetType()

获取当前实例的 Type

(继承自 Object)
HashPasswordForStoringInConfigFile(String, String)
已过时.

根据指定的密码和哈希算法生成一个适合于存储在配置文件中的哈希密码。

Initialize()

根据应用程序的配置设置初始化 FormsAuthentication 对象。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
RedirectFromLoginPage(String, Boolean)

将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

RedirectFromLoginPage(String, Boolean, String)

使用 Forms 身份验证 Cookie 的指定 Cookie 路径,将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

RedirectToLoginPage()

将浏览器重定向到登录 URL。

RedirectToLoginPage(String)

将浏览器重定向到带有指定查询字符串的登录 URL。

RenewTicketIfOld(FormsAuthenticationTicket)

有条件地更新 FormsAuthenticationTicket 的发出日期和时间以及过期日期和时间。

SetAuthCookie(String, Boolean)

为提供的用户名创建一个身份验证票证,并将该票证添加到响应的 Cookie 集合中或 URL 中(如果使用的是无 Cookie 身份验证)。

SetAuthCookie(String, Boolean, String)

为提供的用户名创建一个身份验证票证,并使用提供的 cookie 路径或使用 URL(如果使用的是无 Cookie 身份验证)将该票证添加到响应的 Cookie 集合中。

SignOut()

从浏览器删除 Forms 身份验证票证。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅