This documentation is archived and is not being maintained.

How to: Create 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.

To create a GenericPrincipal object

  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.

    Dim MyIdentity As New GenericIdentity("MyUser")

    GenericIdentity MyIdentity = new GenericIdentity("MyUser");
  2. 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.

    Dim MyStringArray As String() = {"Manager", "Teller"}
    DIm MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)

    String[] MyStringArray = {"Manager", "Teller"};
    GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray);
  3. 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

    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 objects to the console.

No code example is currently available or this language may not be supported.

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