System.Security.PrincipalSystem.Security.PrincipalSystem.Security.Permissions.dll
Represents a security identifier (SID). A security identifier is a unique value of variable length that is used to identify a trust policy in a Windows security access control list.
The SecurityIdentifier class is used to represent security identifiers for principals, such as users and groups.
[SerializableAttribute]
public sealed class SecurityIdentifier : IdentityReference
System.Object
System.Security.Principal.IdentityReference
System.Security.Principal.SecurityIdentifier
Public static members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.
A security identifier (SID) is a unique value that is used to identify a security principal, such as a user account or a group account, or a security principalfacility.
The SecurityIdentifier class provides methods to work with SIDs, including constructing them from their string representation, comparing them, and converting them to other formats.
You can obtain a SecurityIdentifier object for the current user or for well-known security principals.
Initializes a new instance of the SecurityIdentifier class using the specified string representation of a security identifier.
Initializes a new instance of the SecurityIdentifier class using the specified binary form of a security identifier and the offset within the array.
Initializes a new instance of the SecurityIdentifier class using the specified well-known security identifier type and the security identifier of the domain.
Returns true if the security identifier represents a user account; otherwise, returns false.
Compares the current SecurityIdentifier object with another IdentityReference object and returns true if they are equal and false otherwise.
Returns the hash code for the current SecurityIdentifier object.
Returns the string representation of the security identifier.
Retrieves a collection of IdentityReference objects that are referenced by the IdentityReference object.
Gets the value of the relative identifier (RID) portion of the security identifier.
Gets the parent security identifiers of the current security identifier.
Gets the string representation of the security identifier.
The following code example shows how to create a SecurityIdentifier object and use some of its properties and methods.
using System;
using System.Security.Principal;
public class SecurityIdentifierExample
{
public static void Main(string[] args)
{
// Get the SID for the current user
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
SecurityIdentifier sid = (SecurityIdentifier)currentUser.User;
Console.WriteLine($"User SID: {sid.Value}");
Console.WriteLine($"Is Account SID: {sid.IsAccountSid()}");
Console.WriteLine($"SID Type: {sid.ToString().Substring(0, sid.ToString().IndexOf('-'))}"); // Basic attempt to show type
// Example of a well-known SID
SecurityIdentifier adminSid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
Console.WriteLine($"Administrators SID: {adminSid.Value}");
// Comparing SIDs
if (sid.CompareTo(adminSid) == 0)
{
Console.WriteLine("Current user is an administrator.");
}
else
{
Console.WriteLine("Current user is not an administrator.");
}
}
}