FormsAuthentication 類別

定義

管理網頁應用程式的表單認證服務。 此類別無法獲得繼承。

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 應用程式的登入頁面。

重要事項

此範例包含一個接受使用者輸入的文字框,這可能構成安全威脅。 預設情況下,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 驗證 的網頁應用程式能夠進行使用者與密碼驗證。 透過表單認證,使用者資訊會儲存在外部資料來源中,例如 Membership 資料庫,或應用程式的設定檔中。 一旦使用者獲得認證,表單認證會在 Cookie 或網址中維護認證單,讓已認證的使用者無需在每次請求時提供憑證。

表單認證可透過將認證設定元素的屬性設定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。 也有方法可以管理表單認證工單,如果需要的話。

建構函式

名稱 Description
FormsAuthentication()

初始化 FormsAuthentication 類別的新執行個體。

屬性

名稱 Description
CookieDomain

取得表單認證 cookie 網域的值。

CookieMode

會獲得一個值,指示應用程式是否已設定為無 cookie 表單認證。

CookieSameSite

取得或設定 Cookie 的 SameSite 屬性值。

CookiesSupported

會獲得一個值,表示應用程式是否設定支援無 Cookie 表單認證。

DefaultUrl

如果沒有指定重定向 URL,則會取得類別將要重定向的 URL FormsAuthentication

EnableCrossAppRedirects

取得一個值,指示已驗證的使用者是否能被導向到其他網頁應用程式中的 URL。

FormsCookieName

取得用來存放表單認證單的 cookie 名稱。

FormsCookiePath

取得表單認證 cookie 的路徑。

IsEnabled

會得到一個值,表示表單認證是否啟用。

LoginUrl

取得類別會重定向的登入頁面 FormsAuthentication 網址。

RequireSSL

會取得一個值,表示表單認證 cookie 是否需要 SSL 才能回傳到伺服器。

SlidingExpiration

會獲得一個值,表示是否啟用滑動過期功能。

TicketCompatibilityMode

會獲得一個值,指示票券到期日是使用協調世界時(UTC)還是當地時間。

Timeout

它會取得認證工單到期前的期限。

方法

名稱 Description
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()

將瀏覽器重新導向到登入網址。

RedirectToLoginPage(String)

將瀏覽器重新導向到指定的查詢字串登入網址。

RenewTicketIfOld(FormsAuthenticationTicket)

有條件地更新發行日期、時間及到期日與時間。FormsAuthenticationTicket

SetAuthCookie(String, Boolean, String)

為所提供的使用者名稱建立認證工單,並將其加入回應的 cookie 集合中,使用提供的 cookie 路徑,或若使用無 cookie 認證則使用 URL。

SetAuthCookie(String, Boolean)

為所提供的使用者名稱建立認證工單,並將其加入回應的 cookies 集合中,或若使用無 cookie 認證則加入網址。

SignOut()

從瀏覽器移除表單認證工單。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱