How to determine if a string is a User SID?

c#, windows

Solution

According to Security Identifiers description, SID has following form (it has one to fourteen subauthority values):

S-1-<identifier authority>-<sub1>-<sub2>-…-<subn>-<rid>

You can use regular expression to check if string matches this pattern:

string input = "S-5-1-76-1812374880-3438888550-261701130-6117";
string sidPattern = @"^S-\d-\d+-(\d+-){1,14}\d+$";
bool isValidFormat = Regex.IsMatch(input, sidPattern);

That will ensure that input string has valid format, but that will not prove that SID is valid. As suggested in comments, you should try get account if you need to check if you have valid SID.

Problem

In Windows Operating System, user SIDs are represented with a string such as : S-5-1-76-1812374880-3438888550-261701130-6117 Is there any way that I can identify that such a string is a valid User SID? Thanks.

Original source

Related problems