Condividi tramite


Creating GenericPrincipal and GenericIdentity Objects

You can use the GenericIdentity class in conjunction with the GenericPrincipal class to create an authorization scheme that exists independent of a Windows NT or Windows 2000 domain.

Perform the following tasks to create an instance of the GenericPrincipal class.

  1. Create a new instance of the identity class and initialize it with the name you want it to hold. The following code creates a new GenericIdentity object and initializes it with the name MyUser.

    GenericIdentity MyIdentity = new GenericIdentity("MyUser");
    [Visual Basic]Dim MyIdentity As New GenericIdentity("MyUser")
    
  2. Next, create a new instance of the GenericPrincipal class and initialize it with the previously created GenericIdentity object and an array of strings that represent the roles that you want associated with this principal. The following code example specifies an array of strings that represent an administrator role and a user role. The GenericPrincipal is then initialized with the previous GenericIdentity and the string array.

    String[] MyStringArray = {"Manager", "Teller"};
    GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray);
    [Visual Basic]Dim MyStringArray As String() = {"Manager", "Teller"}
    DIm MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)
    
  3. Finally, use the following code to attach the principal to the current thread. This is valuable in situations where the principal must be validated several times, it must be validated by other code running in your application, or it must be validated by a PrincipalPermission object. You can still perform role-based validation on the principal object without attaching it to the thread. For more information, see Replacing a Principal Object.

    Thread.CurrentPrincipal = MyPrincipal;
    [Visual Basic]Thread.CurrentPrincipal = MyPrincipal
    

The following code example demonstrates how to create an instance of a GenericPrincipal and a GenericIdentity. This code displays the values of these classes to the console.

   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;
        }
    }
[Visual Basic]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

When executed, the application displays output similar to the following.

The Name is: MyIdentity
The IsAuthenticated is: True
Is this a Manager? True

See Also

GenericIdentity | GenericPrincipal | Replacing a Principal Object | Principal and Identity Objects | PrincipalPermission