J Weezy
J Weezy

Reputation: 3957

Active Directory: Get RID from SID

I am attempting to retrieve the relative identifier (RID) from the security identifier (SID), which as I understand is the last sequence of numbers on the SID. I received a code example that uses regular expressions to retrieve the value, but I am new to RegEx (see link below). So, before I go using it without fully understanding it, my question is this: is there a reason to use RegEx as opposed to just getting the right most value before the last '-' in the string?

        // Retrieve the RID from the SID.
        string strObjectSid = "S-1-5-21-397955417-62688126-188441444-1010";
        var reg = new Regex( @"^S.*-(\d+)$" );
        var match = reg.Match(strObjectSid);
        var rid = match.Groups[1].Value;

The expected result for rid is 1010.

Active Directory: DirectoryEntry member list <> GroupPrincipal.GetMembers()

In addition, I tried retrieving the RID attribute from AD but it is not available. Microsoft does not provide any method for retrieving the RID. I know this may seem like a silly question, but this value is important for what I am trying to accomplish, so it needs to be correct along with whatever method I implement to retrieve it.

Upvotes: 1

Views: 1614

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40958

There is no reason it can't be done without RegEx. I copied that from somewhere else too.

That regular expression just tells it to look for a grouping of digits after a dash that occurs just before the end of the string.

So, essentially, the last group of digits.

The same can be done with Substring (search for the last dash, add 1 and return everything from that position to the end):

strObjectSid.Substring(strObjectSid.LastIndexOf('-') + 1)

Upvotes: 2

Related Questions