MuhKuh
MuhKuh

Reputation: 376

Is sid string a well-known sid

Is there a fast way (without copying all well-known sids into the code) to determine if a sid string is a well-known one?

bool IsWellKnownSid(string sid)

Upvotes: 0

Views: 852

Answers (2)

Gabriel Luci
Gabriel Luci

Reputation: 40958

You can use the WellKnownSidType enum, like Austin mentioned, but you'll still have to loop through each value to check if it matches.

Here is a method that would do that:

private static WellKnownSidType[] _wellKnownSids = (WellKnownSidType[])Enum.GetValues(typeof(WellKnownSidType));

public bool IsWellKnownSid(SecurityIdentifier sid) {
    foreach (var wellKnown in _wellKnownSids) {
        if (sid.IsWellKnown(wellKnown)) return true;
    }
    return false;
}

The _wellKnownSids field is to prevent you from calling Enum.GetValues every single time you call the method when the values will always be the same.

You can change that into an extension method if you prefer:

public static bool IsWellKnownSid(this SecurityIdentifier sid)

Upvotes: 1

Austin T French
Austin T French

Reputation: 5140

It appears you could use Microsot's ENUM:

https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.wellknownsidtype?view=dotnet-plat-ext-3.1

A quick and dirty approach that seems to work at a basic level without much real testing:

var account = System.Security.Principal.WellKnownSidType.AccountCertAdminsSid;
var reference = new System.Security.Principal.SecurityIdentifier("S-1-0-0"); 
Console.WriteLine(reference.IsWellKnown(account));

Upvotes: 0

Related Questions