Postupy: Vytvoření objektů GenericPrincipal a GenericIdentity
Pro vytvoření schématu autorizace, které existuje nezávisle na doméně systému Windows NT nebo Windows 2000 můžete použít třídu GenericIdentity ve spojení s třídou GenericPrincipal.
Vytvoření objektu GenericPrincipal
Vytvořte novou instanci třídy identity a inicializujte ji s názvem, který si chcete podržet. Následující kód vytvoří nový objekt GenericIdentity a inicializuje ho názvem MyUser.
Dim MyIdentity As New GenericIdentity("MyUser")
GenericIdentity MyIdentity = new GenericIdentity("MyUser");
Vytvořte novou instanci třídy GenericPrincipal a inicializujte ji s dříve vytvořeným objektem GenericIdentity a polem řetězců, které představují role, které chcete asociovat s tímto objektem zabezpečení. Následující příklad kódu specifikuje pole řetězců, které reprezentují roli správce a roli uživatele. Objekt GenericPrincipal je potom inicializován předchozím GenericIdentity a polem řetězců.
Dim MyStringArray As String() = {"Manager", "Teller"} DIm MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)
String[] MyStringArray = {"Manager", "Teller"}; GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray);
Použijte následující kód pro připojení objektu zabezpečení k aktuálnímu vláknu. Toto je důležité v situacích, kde objekt zabezpečení musí být ověřen několikrát. Musí být ověřen jiným kódem běžícím ve vaší aplikaci nebo musí být ověřen objektem PrincipalPermission. Stále můžete na objektu zabezpečení provádět ověřování založené na rolích bez jeho připojení k vláknu. Další informace naleznete v tématu Replacing a Principal Object.
Thread.CurrentPrincipal = MyPrincipal
Thread.CurrentPrincipal = MyPrincipal;
Příklad
Následující příklad kódu ukazuje, jak vytvořit instanci GenericPrincipal a GenericIdentity. Tento kód zobrazuje hodnoty těchto objektů v konzoli.
Imports System
Imports System.Security.Principal
Imports System.Threading
Public Class Class1
Public Shared Sub Main()
' Create generic identity.
Dim MyIdentity As New GenericIdentity("MyIdentity")
' Create generic principal.
Dim MyStringArray As String() = {"Manager", "Teller"}
Dim MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)
' Attach the principal to the current thread.
' This is not required unless repeated validation must occur,
' other code in your application must validate, or the
' PrincipalPermisson object is used.
Thread.CurrentPrincipal = MyPrincipal
' Print values to the console.
Dim Name As String = MyPrincipal.Identity.Name
Dim Auth As Boolean = MyPrincipal.Identity.IsAuthenticated
Dim IsInRole As Boolean = MyPrincipal.IsInRole("Manager")
Console.WriteLine("The Name is: {0}", Name)
Console.WriteLine("The IsAuthenticated is: {0}", Auth)
Console.WriteLine("Is this a Manager? {0}", IsInRole)
End Sub
End Class
using System;
using System.Security.Principal;
using System.Threading;
public class Class1
{
public static int Main(string[] args)
{
// Create generic identity.
GenericIdentity MyIdentity = new GenericIdentity("MyIdentity");
// Create generic principal.
String[] MyStringArray = {"Manager", "Teller"};
GenericPrincipal MyPrincipal =
new GenericPrincipal(MyIdentity, MyStringArray);
// Attach the principal to the current thread.
// This is not required unless repeated validation must occur,
// other code in your application must validate, or the
// PrincipalPermisson object is used.
Thread.CurrentPrincipal = MyPrincipal;
// Print values to the console.
String Name = MyPrincipal.Identity.Name;
bool Auth = MyPrincipal.Identity.IsAuthenticated;
bool IsInRole = MyPrincipal.IsInRole("Manager");
Console.WriteLine("The Name is: {0}", Name);
Console.WriteLine("The IsAuthenticated is: {0}", Auth);
Console.WriteLine("Is this a Manager? {0}", IsInRole);
return 0;
}
}
Po spuštění aplikace zobrazí výstup podobný následujícímu.
The Name is: MyIdentity
The IsAuthenticated is: True
Is this a Manager? True