WindowsIdentity Class

Represents the identity of the Windows user that is associated with a thread.

Syntax

public sealed class WindowsIdentity : IIdentity, IDisposable

Remarks

The WindowsIdentity class represents the identity of the Windows user that is associated with a thread. You can use the WindowsIdentity class to obtain information about the current user, such as their name, authentication type, and the security groups they belong to. This class is particularly useful in applications that require Windows authentication, such as ASP.NET applications running on IIS.

The WindowsIdentity object is created by the system when a Windows user logs in or when an identity is explicitly asserted. You can retrieve the WindowsIdentity for the current thread using the static GetCurrent() method.

Constructors

WindowsIdentity(string sName) Initializes a new instance of the WindowsIdentity class with the specified name.
WindowsIdentity(string sName, string sAuthType) Initializes a new instance of the WindowsIdentity class with the specified name and authentication type.
WindowsIdentity(IntPtr userToken) Initializes a new instance of the WindowsIdentity class with the specified user token.
WindowsIdentity(IntPtr userToken, string authType) Initializes a new instance of the WindowsIdentity class with the specified user token and authentication type.

Properties

Name Description
AuthenticationType Gets the type of authentication used for the current Windows user.
Claims Gets a collection of claims associated with the identity.
IsAuthenticated Gets a value indicating whether the user has been authenticated.
IsGuest Gets a value indicating whether the current user is a guest.
IsSystem Gets a value indicating whether the current user is the system.
IsAnonymous Gets a value indicating whether the current user is anonymous.
Name Gets the name of the current Windows user.
Owner Gets the owner of the security descriptor for the user token.
Token Gets the handle to the Windows user token.

Methods

Dispose() Releases all resources used by the current instance of the WindowsIdentity class.
Equals(object obj) Determines whether the specified object is equal to the current object.
GetHashCode() Serves as the default hash function.
GetCurrent() Returns a WindowsIdentity object that represents the current Windows user.
Impersonate() Impersonates the Windows user represented by the current WindowsIdentity object.
ToString() Returns a string that represents the current object.

Example

The following example demonstrates how to get the current WindowsIdentity and display its properties.

using System;
using System.Security.Principal;

public class Example
{
    public static void Main(string[] args)
    {
        try
        {
            WindowsIdentity currentUser = WindowsIdentity.GetCurrent();

            Console.WriteLine($"User Name: {currentUser.Name}");
            Console.WriteLine($"Authentication Type: {currentUser.AuthenticationType}");
            Console.WriteLine($"Is Authenticated: {currentUser.IsAuthenticated}");
            Console.WriteLine($"Is System: {currentUser.IsSystem}");
            Console.WriteLine($"Is Anonymous: {currentUser.IsAnonymous}");
            Console.WriteLine($"Is Guest: {currentUser.IsGuest}");

            // Get user's groups
            WindowsPrincipal principal = new WindowsPrincipal(currentUser);
            Console.WriteLine("\nMember of groups:");
            foreach (string group in Enum.GetNames(typeof(WindowsBuiltInRole)))
            {
                if (principal.IsInRole((WindowsBuiltInRole)Enum.Parse(typeof(WindowsBuiltInRole), group)))
                {
                    Console.WriteLine($"- {group}");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

See Also