SecurityIdentifier Class

Declaring type: System.Security.Principal
Namespace: System.Security.Principal
Assembly: System.Security.Permissions.dll

Overview

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.

Syntax

[SerializableAttribute]
public sealed class SecurityIdentifier : IdentityReference

Inheritance Hierarchy

System.Object
System.Security.Principal.IdentityReference
System.Security.Principal.SecurityIdentifier

Thread Safety

Public static members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.

Remarks

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.

Constructors

Public Constructors

  • public SecurityIdentifier(string sid)

    Initializes a new instance of the SecurityIdentifier class using the specified string representation of a security identifier.

  • public SecurityIdentifier(byte[] binaryForm, int offset)

    Initializes a new instance of the SecurityIdentifier class using the specified binary form of a security identifier and the offset within the array.

  • public SecurityIdentifier(WellKnownSidType sidType, IdentityReference domainSid)

    Initializes a new instance of the SecurityIdentifier class using the specified well-known security identifier type and the security identifier of the domain.

Methods

Public Methods

  • public override bool IsAccountSid()

    Returns true if the security identifier represents a user account; otherwise, returns false.

  • public override bool IsEqual(IdentityReference rid)

    Compares the current SecurityIdentifier object with another IdentityReference object and returns true if they are equal and false otherwise.

  • public override int GetHashCode()

    Returns the hash code for the current SecurityIdentifier object.

  • public override string ToString()

    Returns the string representation of the security identifier.

  • public override IdentityReferenceCollection GetReferencedIdentities()

    Retrieves a collection of IdentityReference objects that are referenced by the IdentityReference object.

Properties

Public Properties

  • public override int Value

    Gets the value of the relative identifier (RID) portion of the security identifier.

  • public override IdentityReferenceCollection Parents

    Gets the parent security identifiers of the current security identifier.

  • public override string Value

    Gets the string representation of the security identifier.

Example

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.");
        }
    }
}
SecurityIdentifier System.Security.Principal SID Windows Security Access Control IdentityReference