FormsIdentity Klasa

Definicja

Reprezentuje tożsamość użytkownika uwierzytelnianą przy użyciu uwierzytelniania formularzy. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
FormsIdentity
Dziedziczenie
FormsIdentity
Atrybuty
Implementuje

Przykłady

Poniższy przykład kodu generuje element FormsAuthenticationTicket , a następnie używa go do utworzenia wystąpienia FormsIdentity klasy, na podstawie tego, czy użytkownik dostarcza prawidłową nazwę użytkownika i hasło w formularzu.

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

Uwagi

Klasa FormsIdentity jest używana przez FormsAuthenticationModule użytkownika podczas uwierzytelniania formularzy. Wystąpienie FormsIdentity klasy jest tworzone przy użyciu FormsAuthenticationTicket pliku cookie uwierzytelniania formularzy lub adresu URL. Nowe wystąpienie FormsIdentity klasy służy do konstruowania nowego GenericPrincipal obiektu ustawionego jako wartość User właściwości dla bieżącego HttpContextobiektu .

Konstruktory

FormsIdentity(FormsAuthenticationTicket)

Inicjuje nowe wystąpienie klasy FormsIdentity.

FormsIdentity(FormsIdentity)

Inicjuje FormsIdentity nowe wystąpienie klasy na podstawie określonej tożsamości.

Pola

DefaultIssuer

Domyślny wystawca; "URZĄD LOKALNY".

(Odziedziczone po ClaimsIdentity)
DefaultNameClaimType

Domyślny typ oświadczenia nazwy; Name.

(Odziedziczone po ClaimsIdentity)
DefaultRoleClaimType

Domyślny typ oświadczenia roli; Role.

(Odziedziczone po ClaimsIdentity)

Właściwości

Actor

Pobiera lub ustawia tożsamość strony wywołującej, która otrzymała prawa delegowania.

(Odziedziczone po ClaimsIdentity)
AuthenticationType

Pobiera typ uwierzytelnionej tożsamości.

BootstrapContext

Pobiera lub ustawia token użyty do utworzenia tej tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
Claims

Pobiera kolekcję oświadczeń skojarzonych z tą tożsamością.

CustomSerializationData

Zawiera wszelkie dodatkowe dane dostarczone przez typ pochodny. Zazwyczaj ustawiane podczas wywoływania metody WriteTo(BinaryWriter, Byte[]).

(Odziedziczone po ClaimsIdentity)
IsAuthenticated

Pobiera wartość wskazującą, czy nastąpiło uwierzytelnianie.

Label

Pobiera lub ustawia etykietę dla tej tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
Name

Pobiera nazwę użytkownika tożsamości formularzy.

NameClaimType

Pobiera typ oświadczenia używany do określania, które oświadczenia zapewniają wartość dla Name właściwości tej tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
RoleClaimType

Pobiera typ oświadczenia, który będzie interpretowany jako rola platformy .NET wśród oświadczeń w tej tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
Ticket

Pobiera element FormsAuthenticationTicket dla tożsamości użytkownika uwierzytelniania formularzy.

Metody

AddClaim(Claim)

Dodaje pojedyncze oświadczenie do tej tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
AddClaims(IEnumerable<Claim>)

Dodaje listę oświadczeń do tej tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
Clone()

Pobiera kopię bieżącego FormsIdentity wystąpienia.

CreateClaim(BinaryReader)

Udostępnia punkt rozszerzalności dla typów pochodnych w celu utworzenia niestandardowego Claimelementu .

(Odziedziczone po ClaimsIdentity)
Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
FindAll(Predicate<Claim>)

Pobiera wszystkie oświadczenia, które są zgodne z określonym predykatem.

(Odziedziczone po ClaimsIdentity)
FindAll(String)

Pobiera wszystkie oświadczenia, które mają określony typ oświadczenia.

(Odziedziczone po ClaimsIdentity)
FindFirst(Predicate<Claim>)

Pobiera pierwsze oświadczenie zgodne z określonym predykatem.

(Odziedziczone po ClaimsIdentity)
FindFirst(String)

Pobiera pierwsze oświadczenie z określonym typem oświadczenia.

(Odziedziczone po ClaimsIdentity)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetObjectData(SerializationInfo, StreamingContext)

Wypełnia dane SerializationInfo wymagane do serializacji bieżącego ClaimsIdentity obiektu.

(Odziedziczone po ClaimsIdentity)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
HasClaim(Predicate<Claim>)

Określa, czy ta tożsamość oświadczenia ma oświadczenie zgodne z określonym predykatem.

(Odziedziczone po ClaimsIdentity)
HasClaim(String, String)

Określa, czy ta tożsamość oświadczenia ma oświadczenie o określonym typie oświadczenia i wartości.

(Odziedziczone po ClaimsIdentity)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
RemoveClaim(Claim)

Próbuje usunąć oświadczenie z tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)
TryRemoveClaim(Claim)

Próbuje usunąć oświadczenie z tożsamości oświadczeń.

(Odziedziczone po ClaimsIdentity)
WriteTo(BinaryWriter)

Serializuje przy użyciu klasy BinaryWriter.

(Odziedziczone po ClaimsIdentity)
WriteTo(BinaryWriter, Byte[])

Serializuje przy użyciu klasy BinaryWriter.

(Odziedziczone po ClaimsIdentity)

Dotyczy

Zobacz też