Export (0) Print
Expand All

GenericIdentity Class

Represents a generic user.

Namespace: System.Security.Principal
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class GenericIdentity : IIdentity
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class GenericIdentity implements IIdentity
SerializableAttribute 
ComVisibleAttribute(true) 
public class GenericIdentity implements IIdentity

An identity object represents the user on whose behalf the code is running.

The following example shows the use of members of the GenericIdentity class.

using System;
using System.Security.Principal;

class GenericIdentityMembers
{
    [STAThread]
    static void Main(string[] args)
    {
        // Create a GenericIdentity object with no authentication type 
        // specified.
        GenericIdentity defaultIdentity = new GenericIdentity("DefaultUser");

        // Retrieve a GenericIdentity created from current WindowsIdentity
        // values.
        GenericIdentity currentIdentity = GetGenericIdentity();

        ShowIdentityPreferences(new GenericIdentity(""));
        ShowIdentityPreferences(defaultIdentity);
        ShowIdentityPreferences(currentIdentity);

        Console.WriteLine("The sample completed successfully; " +
            "press Enter to continue.");
        Console.ReadLine();
    }

    // Print identity preferences to the console window.
    private static void ShowIdentityPreferences(
        GenericIdentity genericIdentity)
    {
        // Retrieve the name of the generic identity object.
        string identityName = genericIdentity.Name;

        // Retrieve the authentication type of the generic identity object.
        string identityAuthenticationType = 
            genericIdentity.AuthenticationType;

        Console.WriteLine("Name: " + identityName);
        Console.WriteLine("Type: " + identityAuthenticationType);
        
        // Verify that the user's identity has been authenticated
        // (was created with a valid name).
        if (genericIdentity.IsAuthenticated)
        {
            Console.WriteLine("The user's identity has been authenticated.");
        }
        else
        {
            Console.WriteLine("The user's identity has not been " + 
                "authenticated.");
        }
        Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    // Create generic identity based on values from the current
    // WindowsIdentity.
    private static GenericIdentity GetGenericIdentity()
    {
        // Get values from the current WindowsIdentity.
        WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

        // Construct a GenericIdentity object based on the current Windows
        // identity name and authentication type.
        string authenticationType = windowsIdentity.AuthenticationType;
        string userName = windowsIdentity.Name;
        GenericIdentity authenticatedGenericIdentity =
            new GenericIdentity(userName, authenticationType);

        return authenticatedGenericIdentity;
    }
}



import System.*;
import System.Security.Principal.*;

class GenericIdentityMembers
{
    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        // Create a GenericIdentity object with no authentication type 
        // specified.
        GenericIdentity defaultIdentity = new GenericIdentity("DefaultUser");
        // Retrieve a GenericIdentity created from current WindowsIdentity
        // values.
        GenericIdentity currentIdentity = GetGenericIdentity();

        ShowIdentityPreferences(new GenericIdentity(""));
        ShowIdentityPreferences(defaultIdentity);
        ShowIdentityPreferences(currentIdentity);

        Console.WriteLine("The sample completed successfully; " 
            + "press Enter to continue.");
        Console.ReadLine();
    } //main

    // Print identity preferences to the console window.
    private static void ShowIdentityPreferences(GenericIdentity genericIdentity)
    {
        // Retrieve the name of the generic identity object.
        String identityName = genericIdentity.get_Name();
        // Retrieve the authentication type of the generic identity object.
        String identityAuthenticationType 
            = genericIdentity.get_AuthenticationType();
        Console.WriteLine("Name: " + identityName);
        Console.WriteLine("Type: " + identityAuthenticationType);
        // Verify that the user's identity has been authenticated
        // (was created with a valid name).
        if (genericIdentity.get_IsAuthenticated()) {
            Console.WriteLine("The user's identity has been authenticated.");
        }
        else {
            Console.WriteLine("The user's identity has not been " 
                + "authenticated.");
        }
        Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~");
    } //ShowIdentityPreferences

    // Create generic identity based on values from the current
    // WindowsIdentity.
    private static GenericIdentity GetGenericIdentity()
    {
        // Get values from the current WindowsIdentity.
        WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
        // Construct a GenericIdentity object based on the current Windows
        // identity name and authentication type.
        String authenticationType = windowsIdentity.get_AuthenticationType();
        String userName = windowsIdentity.get_Name();
        GenericIdentity authenticatedGenericIdentity 
            = new GenericIdentity(userName, authenticationType);
        return authenticatedGenericIdentity;
    } //GetGenericIdentity
} //GenericIdentityMembers

System.Object
  System.Security.Principal.GenericIdentity
     System.Net.HttpListenerBasicIdentity

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft