FormsIdentity 類別

定義

表示使用表單驗證的已驗證使用者識別。 此類別無法獲得繼承。

public ref class FormsIdentity sealed : System::Security::Principal::IIdentity
public ref class FormsIdentity : System::Security::Principal::IIdentity
public ref class FormsIdentity : System::Security::Claims::ClaimsIdentity
[System.Serializable]
public sealed class FormsIdentity : System.Security.Principal.IIdentity
[System.Serializable]
public class FormsIdentity : System.Security.Principal.IIdentity
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(false)]
public class FormsIdentity : System.Security.Claims.ClaimsIdentity
[<System.Serializable>]
type FormsIdentity = class
    interface IIdentity
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(false)>]
type FormsIdentity = class
    inherit ClaimsIdentity
Public NotInheritable Class FormsIdentity
Implements IIdentity
Public Class FormsIdentity
Implements IIdentity
Public Class FormsIdentity
Inherits ClaimsIdentity
繼承
FormsIdentity
繼承
FormsIdentity
屬性
實作

範例

下列程式碼範例會產生 FormsAuthenticationTicket ,然後根據使用者是否在表單上提供正確的使用者名稱和密碼,使用它來建立 類別的實例 FormsIdentity

<%@ Page Language="C#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">
    protected void Login_Click(object sender, EventArgs e)
    {
        bool isAuthenticated = false;
        if (string.Compare(UserNameTextBox.Text, "UserName", true,
        CultureInfo.InvariantCulture) == 0)
        {
            if (string.Compare(PasswordTextBox.Text, "Password", true,
            CultureInfo.InvariantCulture) == 0)
            {
                isAuthenticated = true;
            }
        }
        else isAuthenticated = false;
        
        // Create the formsIdentity for the user.
        CreateformsIdentity(UserNameTextBox.Text, isAuthenticated);
    }
    private void CreateformsIdentity(string userName, bool isAuthenticated)
    {
        FormsIdentity formsID;
        FormsAuthenticationTicket authenticationTicket;
        
        if (isAuthenticated)
        {
            // If authentication passed, create a ticket 
            // as a Manager that expires in 15 minutes.
            authenticationTicket = new FormsAuthenticationTicket(1, userName,
                DateTime.Now, DateTime.Now.AddMinutes(15), false, "Manager");
        }
        else
        {
            // If authentication failed, create a ticket 
            // as a guest that expired 5 minutes ago.
            authenticationTicket = new FormsAuthenticationTicket(1, userName,
                DateTime.Now, DateTime.Now.Subtract(new TimeSpan(0, 5, 0)),
                false, "Guest");
        }

        // Create form identity from FormsAuthenticationTicket.
        formsID = new FormsIdentity(authenticationTicket);
        Response.Clear();
        Response.Write("Authentication Type: " + formsID.AuthenticationType +
            "<BR>");

        // Get FormsAuthenticationTicket from the FormIdentity
        FormsAuthenticationTicket ticket = formsID.Ticket;
        if (ticket.Expired)
        {
            Response.Write("Authentication failed, so the role is set to " +
                ticket.UserData);
        }
        else
        {
            Response.Write("Authentication succeeded, so the role is set to " +
                ticket.UserData);
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>WebForm1</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:Label id="UserIdLabel" runat="server"
                style="left: 144px; position: absolute; top: 160px">
                User-ID:</asp:Label>
            <asp:Label id="PasswordLabel" runat="server"
                style="left: 144px; position: absolute; top: 200px">
                Password:</asp:Label>
            <asp:TextBox id="UserNameTextBox" runat="server"
                style="left: 232px; position: absolute; top: 160px;
                width:182px; height:22px"></asp:TextBox>
            <asp:TextBox id="PasswordTextBox" runat="server"
                style="left: 232px; position: absolute; top: 200px;
                width:181px; height:22px" TextMode="Password">
                </asp:TextBox>
            <asp:Button id="Login" runat="server" Text="Login"
                style="left: 232px; position: absolute; top: 232px; 
                width:100px" OnClick="Login_Click"></asp:Button>
        </form>
    </body>
</html>
<%@ Page Language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
    Private Sub Login_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Login.Click

        ' For the example, the user name must be "UserName" and the password
        ' must be "Password" for authentication to succeed.
        Dim isAuthenticated As Boolean
        If String.Compare(UserNameTextBox.Text, "UserName", True, _
            CultureInfo.InvariantCulture) = 0 Then
            If String.Compare(PasswordTextBox.Text, "Password", True, _
                CultureInfo.InvariantCulture) = 0 Then
                isAuthenticated = True
            End If
        Else
            isAuthenticated = False
        End If

        ' Create the FormsIdentity for the user.
        CreateFormsIdentity(UserNameTextBox.Text, isAuthenticated)

    End Sub

    Private Sub CreateFormsIdentity(ByVal userName As String, _
        ByVal isAuthenticated As Boolean)

        Dim formsId As System.Web.Security.FormsIdentity
        Dim authenticationTicket As _
            System.Web.Security.FormsAuthenticationTicket

        If isAuthenticated Then
            ' If authentication passed, create a ticket 
            ' as a Manager that expires in 15 minutes.
            authenticationTicket = _
                New FormsAuthenticationTicket(1, userName, DateTime.Now, _
                DateTime.Now.AddMinutes(15), False, "Manager")
        Else
            ' If authentication failed, create a ticket 
            ' as a guest that expired 5 minutes ago.
            authenticationTicket = _
                New FormsAuthenticationTicket(1, userName, DateTime.Now, _
                DateTime.Now.Subtract(New TimeSpan(0, 5, 0)), False, "Guest")
        End If

        ' Create form identity from FormsAuthenticationTicket.
        formsId = New FormsIdentity(authenticationTicket)

        Response.Clear()
        Response.Write("Authenticate Type: " & _
            formsId.AuthenticationType & "<BR>")

        ' Get FormsAuthenticationTicket from the FormIdentity
        Dim ticket As FormsAuthenticationTicket = formsId.Ticket()
        If ticket.Expired Then
            Response.Write("Authentication failed, so the role is set to " & _
                ticket.UserData)
        Else
            Response.Write("Authentication succeeded, so the role is set to " & _
                ticket.UserData)
        End If
    End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>WebForm1</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:Label id="UserIdLabel" runat="server"
                style="left: 144px; position: absolute; top: 160px">
                User-ID:</asp:Label>
            <asp:Label id="PasswordLabel" runat="server"
                style="left: 144px; position: absolute; top: 200px">
                Password:</asp:Label>
            <asp:TextBox id="UserNameTextBox" runat="server"
                style="left: 232px; position: absolute; top: 160px;
                width:182px; height:22px"></asp:TextBox>
            <asp:TextBox id="PasswordTextBox" runat="server"
                style="left: 232px; position: absolute; top: 200px;
                width:181px; height:22px" TextMode="Password">
                </asp:TextBox>
            <asp:Button id="Login" runat="server" Text="Login"
                style="left: 232px; position: absolute; top: 232px; 
                width:100px"></asp:Button>
        </form>
    </body>
</html>

備註

當使用者使用表單驗證進行驗證時,就會使用 FormsAuthenticationModule 類別 FormsIdentity 。 使用從表單驗證 Cookie 或 URL 解密的 ,建立 類別 FormsAuthenticationTicketFormsIdentity 實例。 類別的新實例 FormsIdentity 是用來建構新的 GenericPrincipal 物件,該物件設定為 User 目前 HttpContext 的 屬性值。

建構函式

FormsIdentity(FormsAuthenticationTicket)

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

FormsIdentity(FormsIdentity)

根據指定的識別初始化 FormsIdentity 類別的新執行個體。

欄位

DefaultIssuer

預設核發者:「本機授權單位」。

(繼承來源 ClaimsIdentity)
DefaultNameClaimType

預設名稱宣告型別;Name.

(繼承來源 ClaimsIdentity)
DefaultRoleClaimType

預設角色宣告型別;Role.

(繼承來源 ClaimsIdentity)

屬性

Actor

取得或設定已授與委派權限之呼叫方的識別。

(繼承來源 ClaimsIdentity)
AuthenticationType

取得已驗證識別的類型。

BootstrapContext

取得或設定用來建立此宣告識別的權杖。

(繼承來源 ClaimsIdentity)
Claims

取得與這個實體相關聯的宣告集合。

CustomSerializationData

包含衍生類型所提供的任何其他資料。 通常會在呼叫 WriteTo(BinaryWriter, Byte[]) 時設定。

(繼承來源 ClaimsIdentity)
IsAuthenticated

取得值,指出是否已進行驗證。

Label

取得或設定此宣告識別的標籤。

(繼承來源 ClaimsIdentity)
Name

取得表單識別的使用者名稱。

NameClaimType

取得宣告型別,用來判斷哪些宣告為這個宣告識別的 Name 屬性提供值。

(繼承來源 ClaimsIdentity)
RoleClaimType

在此宣告身分識別的宣告中,取得將解譯為 .NET 角色的宣告型別。

(繼承來源 ClaimsIdentity)
Ticket

取得表單驗證使用者識別的 FormsAuthenticationTicket

方法

AddClaim(Claim)

將單一宣告加入至這個宣告識別。

(繼承來源 ClaimsIdentity)
AddClaims(IEnumerable<Claim>)

將宣告清單加入至這個宣告識別。

(繼承來源 ClaimsIdentity)
Clone()

取得目前 FormsIdentity 執行個體的複本。

CreateClaim(BinaryReader)

提供衍生類型的擴充點來建立自訂 Claim

(繼承來源 ClaimsIdentity)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FindAll(Predicate<Claim>)

擷取符合指定述詞的所有宣告。

(繼承來源 ClaimsIdentity)
FindAll(String)

擷取具有指定宣告型別的所有宣告。

(繼承來源 ClaimsIdentity)
FindFirst(Predicate<Claim>)

擷取符合指定述詞的第一個宣告。

(繼承來源 ClaimsIdentity)
FindFirst(String)

擷取含指定宣告型別的第一個宣告。

(繼承來源 ClaimsIdentity)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

將序列化目前 SerializationInfo 物件所需的資料填入 ClaimsIdentity

(繼承來源 ClaimsIdentity)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
HasClaim(Predicate<Claim>)

判斷這個宣告識別是否具有與指定之述詞相符的宣告。

(繼承來源 ClaimsIdentity)
HasClaim(String, String)

判斷這個宣告識別是否具有含指定之型別及值的宣告。

(繼承來源 ClaimsIdentity)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RemoveClaim(Claim)

嘗試從宣告識別移除宣告。

(繼承來源 ClaimsIdentity)
ToString()

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

(繼承來源 Object)
TryRemoveClaim(Claim)

嘗試從宣告識別移除宣告。

(繼承來源 ClaimsIdentity)
WriteTo(BinaryWriter)

使用 BinaryWriter 序列化。

(繼承來源 ClaimsIdentity)
WriteTo(BinaryWriter, Byte[])

使用 BinaryWriter 序列化。

(繼承來源 ClaimsIdentity)

適用於

另請參閱