FormsAuthentication 類別

定義

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

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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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

取得表單驗證 Cookie 的網域值。

CookieMode

取得表示應用程式是否針對 Cookieless 表單驗證進行設定的值。

CookieSameSite

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

CookiesSupported

取得表示應用程式是否設定為支援 Cookieless 表單驗證的值。

DefaultUrl

取得未指定重新導向 URL 時,FormsAuthentication 類別將重新導向的 URL。

EnableCrossAppRedirects

取得值,指出已驗證的使用者是否能重新導向至其他 Web 應用程式中的 URL。

FormsCookieName

取得用來存放表單驗證票證的 Cookie 名稱。

FormsCookiePath

取得表單驗證 Cookie 的路徑。

IsEnabled

取得值,指出表單驗證是否已啟用。

LoginUrl

取得 FormsAuthentication 類別將重新導向的登入網頁 URL。

RequireSSL

取得值,指出表單驗證 Cookie 是否需要 SSL 才能傳回至伺服器。

SlidingExpiration

取得一個指示是否啟用變動到期的值。

TicketCompatibilityMode

取得值,指出要使用 Coordinated Universal Time (UTC) 或當地時間做為票證到期日。

Timeout

取得驗證票證逾期前的時間量。

方法

Authenticate(String, String)
已淘汰.

根據存放於應用程式組態檔中的認證,驗證使用者名稱和密碼。

Decrypt(String)

根據傳遞至方法的已加密表單驗證票證,建立 FormsAuthenticationTicket 物件。

EnableFormsAuthentication(NameValueCollection)

啟用表單驗證。

Encrypt(FormsAuthenticationTicket)

建立包含已加密表單驗證票證 (適用於 HTTP Cookie 中) 的字串。

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)

使用為表單驗證 Cookie 指定的 Cookie 路徑,將已驗證的使用者重新導向回到原先要求的 URL 或預設 URL。

RedirectToLoginPage()

將瀏覽器重新導向至登入 URL。

RedirectToLoginPage(String)

將瀏覽器重新導向至包含指定查詢字串的登入 URL。

RenewTicketIfOld(FormsAuthenticationTicket)

有條件地更新 FormsAuthenticationTicket 的核發日期和時間,以及到期日和時間。

SetAuthCookie(String, Boolean)

為所提供的使用者名稱建立驗證票證,並將該票證加入至回應的 Cookie 集合,或加入至 URL (若使用的是 Cookieless 驗證)。

SetAuthCookie(String, Boolean, String)

為所提供的使用者名稱建立驗證票證,並使用提供的 Cookie 路徑或使用 URL (若是使用 Cookieless 驗證),將該票證加入至回應的 Cookie 集合。

SignOut()

從瀏覽器移除表單驗證票證。

ToString()

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

(繼承來源 Object)

適用於

另請參閱