FormsAuthentication 类

定义

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

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

示例

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

<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 应用程序的登录页。

Important

此示例包含接受用户输入的文本框,这是潜在的安全威胁。 默认情况下,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 中维护身份验证票证,以便经过身份验证的用户不需要为每个请求提供凭据。

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

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

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

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

构造函数

名称 说明
FormsAuthentication()

初始化 FormsAuthentication 类的新实例。

属性

名称 说明
CookieDomain

获取表单身份验证 Cookie 的域的值。

CookieMode

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

CookieSameSite

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

CookiesSupported

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

DefaultUrl

FormsAuthentication获取类在未指定重定向 URL 时重定向到的 URL。

EnableCrossAppRedirects

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

FormsCookieName

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

FormsCookiePath

获取表单身份验证 Cookie 的路径。

IsEnabled

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

LoginUrl

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

RequireSSL

获取一个值,该值指示表单身份验证 Cookie 是否需要 SSL 才能返回到服务器。

SlidingExpiration

获取一个值,该值指示是否启用滑动过期。

TicketCompatibilityMode

获取一个值,该值指示是对票证到期日期使用协调世界时(UTC)还是本地时间。

Timeout

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

方法

名称 说明
Authenticate(String, String)
已过时.

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

Decrypt(String)

FormsAuthenticationTicket基于传递给该方法的加密表单身份验证票证创建对象。

EnableFormsAuthentication(NameValueCollection)

启用表单身份验证。

Encrypt(FormsAuthenticationTicket)

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

Equals(Object)

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

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

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

GetAuthCookie(String, Boolean)

为给定用户名创建身份验证 Cookie。 这不会将 Cookie 设置为传出响应的一部分,因此应用程序可以更好地控制 Cookie 的颁发方式。

GetHashCode()

用作默认哈希函数。

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

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

GetType()

获取当前实例的 Type

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

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

Initialize()

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

MemberwiseClone()

创建当前 Object的浅表副本。

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

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

RedirectFromLoginPage(String, Boolean)

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

RedirectToLoginPage()

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

RedirectToLoginPage(String)

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

RenewTicketIfOld(FormsAuthenticationTicket)

有条件地更新问题日期和时间以及到期 FormsAuthenticationTicket日期和时间。

SetAuthCookie(String, Boolean, String)

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

SetAuthCookie(String, Boolean)

为提供的用户名创建身份验证票证,并将其添加到响应的 Cookie 集合中;如果使用无 Cookie 身份验证,请将其添加到 URL。

SignOut()

从浏览器中删除表单身份验证票证。

ToString()

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

(继承自 Object)

适用于

另请参阅