FormsIdentity 类

定义

表示一个使用 Forms 身份验证进行了身份验证的用户标识。 此类不能被继承。

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>

注解

使用 FormsIdentity 表单身份验证对用户进行身份验证时, FormsAuthenticationModule 类由 使用。 类的 FormsIdentity 实例是使用 FormsAuthenticationTicket 从 forms-authentication Cookie 或 URL 解密的 创建。 类的新实例FormsIdentity用于构造一个新GenericPrincipal对象,该对象设置为当前 HttpContextUser 属性的值。

构造函数

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

获取 Forms 标识的用户名。

NameClaimType

获取用于确定为此声明标识的 Name 属性提供值的声明的声明类型。

(继承自 ClaimsIdentity)
RoleClaimType

获取将解释为此声明标识中声明的 .NET 角色的声明类型。

(继承自 ClaimsIdentity)
Ticket

获取 Forms 身份验证用户标识的 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)

适用于

另请参阅