Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert SID to String in .net

Tags:

c#

.net

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 
like image 734
Major Marcell Avatar asked Jul 20 '12 13:07

Major Marcell


People also ask

How do I convert SID to username?

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.

How do I find Active Directory 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.


2 Answers

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(); 
like image 183
M Afifi Avatar answered Sep 21 '22 23:09

M Afifi


After load the property in directoryEntry ....

var usrId = (byte[])directoryEntry.Properties["objectSid"][0]; var objectID = (new SecurityIdentifier(usrId,0)).ToString(); 
like image 33
Ramiro Mosquera Avatar answered Sep 23 '22 23:09

Ramiro Mosquera