I would like to convert the SID's System.Byte[] type to a String.
My code:
string path = "LDAP://DC=abc,DC=contoso,DC=com"; DirectoryEntry entry = new DirectoryEntry(path); DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = "(&(objectClass=user)(samaccountname=user1))"; results = mySearcher.FindAll(); foreach (SearchResult searchResult in results) { Console.WriteLine(searchResult.Properties["ObjectSID"][0].ToString()); }
I tried with this but it gets the values from the domain I'm currently logged in, and i need from a given domain.
System.Security.Principal.NTAccount(user1) .Translate([System.Security.Principal.SecurityIdentifier]).value
You can use the command line (cmd) to convert SID to username using the wmic command. Using the wmic command to get user account, specify the user SID in the where clause to get a user from SID.
To get AD group SID in the active directory, use the Get-ADGroup cmdlet. The Get-ADGroup cmdlet gets a group account specified by the Identity parameter in the PowerShell script. Next, select the AD group's Name and SID properties in the active directory using the pipe operator.
Take a look at the SecurityIdentifier class. You can then do simple things like,
var sidInBytes = (byte[]) *somestuff* var sid = new SecurityIdentifier(sidInBytes, 0); // This gives you what you want sid.ToString();
After load the property in directoryEntry ....
var usrId = (byte[])directoryEntry.Properties["objectSid"][0]; var objectID = (new SecurityIdentifier(usrId,0)).ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With