SecurityIdentifier Class

  • Supported on windows5.1.2600

Represents a security identifier (SID) that uniquely identifies a user, group, or other security principal.

[SupportedOSPlatform("windows5.1.2600")]
public sealed class SecurityIdentifier : IEquatable<SecurityIdentifier?>

Remarks

A SID is a variable-length structure that uniquely identifies a security principal in Windows. This class provides methods to resolve SIDs to account names and domains.

Example

// Get a well-known SID
var adminSid = SecurityIdentifier.FromWellKnown(WellKnownSidType.WinBuiltinAdministratorsSid);
Assert.Equal("S-1-5-32-544", adminSid.Sid);

// The account name is localized: "BUILTIN\Administrators" on an English version of Windows
Assert.NotEmpty(adminSid.FullName);

// Compare SIDs
using var token = AccessToken.OpenCurrentProcessToken(TokenAccessLevels.Query);
var ownerSid = token.GetOwner();
if (ownerSid == adminSid)
{
    // The owner is the Administrators group, which is the case of an elevated process
}
Implements

Properties

NameDescription
DomainGets the domain name associated with the SID.
FullNameGets the full name in the format "Domain\Name".
NameGets the account name associated with the SID.
SidGets the string representation of the SID (e.g., "S-1-5-21-...").

Gets the domain name associated with the SID.

public string? Domain { get; }

Property value

string? The domain name, or null if the SID could not be resolved.

Exceptions

Win32Exception
The account lookup failed.

Remarks

Resolved on first access, which may query the local security authority or a domain controller.

Gets the full name in the format "Domain\Name".

public string FullName { get; }

Property value

string

Gets the account name associated with the SID.

public string? Name { get; }

Property value

string? The account name, or null if the SID could not be resolved.

Exceptions

Win32Exception
The account lookup failed.

Remarks

Resolved on first access, which may query the local security authority or a domain controller.

Gets the string representation of the SID (e.g., "S-1-5-21-...").

public string Sid { get; }

Property value

string

Methods

NameDescription
Equals(SecurityIdentifier?)Determines whether the specified SecurityIdentifier is equal to the current instance.
Equals(object?)Determines whether the specified object is equal to the current object.
FromWellKnown(WellKnownSidType)Creates a SecurityIdentifier for a well-known SID type.
GetHashCode()Serves as the default hash function.
ToString()Returns a string representation of the security identifier.

Equals overloads

Equals(SecurityIdentifier?)

SecurityIdentifier.cs

Determines whether the specified SecurityIdentifier is equal to the current instance.

public bool Equals(SecurityIdentifier? other)
Parameters
other SecurityIdentifier?
Returns

bool

Equals(object?)

SecurityIdentifier.cs

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current object.
Returns

bool true if the specified object is equal to the current object; otherwise, false.

FromWellKnown(WellKnownSidType)

SecurityIdentifier.cs

Creates a SecurityIdentifier for a well-known SID type.

public static SecurityIdentifier FromWellKnown(WellKnownSidType type)

Parameters

type WellKnownSidType
The well-known SID type to create.

Returns

SecurityIdentifier A SecurityIdentifier representing the well-known SID.

Exceptions

Win32Exception
The SID could not be created.

Example

var adminSid = SecurityIdentifier.FromWellKnown(WellKnownSidType.WinBuiltinAdministratorsSid);
var systemSid = SecurityIdentifier.FromWellKnown(WellKnownSidType.WinLocalSystemSid);
Assert.Equal("S-1-5-32-544", adminSid.Sid);
Assert.Equal("S-1-5-18", systemSid.Sid);

Serves as the default hash function.

public override int GetHashCode()

Returns

int A hash code for the current object.

Returns a string representation of the security identifier.

public override string ToString()

Returns

string The full name if available; otherwise, the SID string.

Operators

NameDescription
operator ==(SecurityIdentifier?, SecurityIdentifier?)
operator !=(SecurityIdentifier?, SecurityIdentifier?)

operator ==(SecurityIdentifier?, SecurityIdentifier?)

SecurityIdentifier.cs
public static bool operator ==(SecurityIdentifier? left, SecurityIdentifier? right)

Parameters

left SecurityIdentifier?
right SecurityIdentifier?

Returns

bool

No documentation is available.

operator !=(SecurityIdentifier?, SecurityIdentifier?)

SecurityIdentifier.cs
public static bool operator !=(SecurityIdentifier? left, SecurityIdentifier? right)

Parameters

left SecurityIdentifier?
right SecurityIdentifier?

Returns

bool

No documentation is available.