IIdentity Interface
Represents the identity of a principal. The identity is the information a user has about themselves.
Represents the identity of a principal. The identity is the information a user has about themselves.
The IIdentity interface represents the identity of a principal. It provides basic information about the authenticated user, such as their name and authentication type. This interface is fundamental to Windows authentication and authorization in .NET applications.
Key aspects of the IIdentity interface:
| Name | Description |
|---|---|
Name |
Gets the name of the current user. |
AuthenticationType |
Gets the type of authentication that was used to authenticate the user. |
IsAuthenticated |
Gets a value indicating whether the identity has been authenticated. |
The IIdentity interface is typically implemented by classes that represent user identities in a security context. The most common implementation in Windows environments is WindowsIdentity, which encapsulates Windows security information. For web applications, FormsIdentity is often used with ASP.NET Forms Authentication.
The IIdentity interface is crucial for determining the privileges and permissions associated with a user. It forms the basis for the IPrincipal interface, which represents the security's principal object and contains an IIdentity object.
When working with security in .NET, you will frequently encounter the IIdentity interface through the Thread.CurrentPrincipal.Identity property or by casting the current principal to a specific identity type.
The following C# code example demonstrates how to access and use the IIdentity interface to retrieve information about the current user.
using System;
using System.Security.Principal;
using System.Threading;
public class IdentityExample
{
public static void Main()
{
// Get the current principal's identity
IIdentity currentIdentity = Thread.CurrentPrincipal.Identity;
if (currentIdentity != null)
{
Console.WriteLine($"Authentication Type: {currentIdentity.AuthenticationType}");
Console.WriteLine($"Is Authenticated: {currentIdentity.IsAuthenticated}");
Console.WriteLine($"Name: {currentIdentity.Name}");
// Example of checking for a specific type of identity
if (currentIdentity is WindowsIdentity)
{
WindowsIdentity windowsIdentity = (WindowsIdentity)currentIdentity;
Console.WriteLine("This is a WindowsIdentity.");
// You can access Windows-specific properties here
}
else if (currentIdentity is FormsIdentity)
{
FormsIdentity formsIdentity = (FormsIdentity)currentIdentity;
Console.WriteLine("This is a FormsIdentity.");
// You can access Forms-specific properties here
}
}
else
{
Console.WriteLine("No identity is currently set.");
}
}
}
This interface is part of the System.Security.Principal namespace.
System.ObjectDerived classes include:
System.Security.Principal.GenericIdentitySystem.Security.Principal.WindowsIdentitySystem.Web.Security.FormsIdentity