How to convert SID to String in .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)

I tried with this but it gets the values from the domain I'm currently logged in, and i need from a given domain.



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

This is what ive done , after some reading it seemed safer to store the value in oct. If you dont know which servers is on the other side. The code below shows how to do it to get your desired result

private static string ExtractSinglePropertyValueFromByteArray(object value)
    //all if checks etc has been omitted
    string propertyValue = string.Empty;
    var bytes = (byte[])value;
    var propertyValueOct = BuildOctString(bytes); // 010500....etc
    var propertyValueSec = BuildSecString(bytes); // S-1-5-...etc
    propertyValue = propertyValueSec;
    return propertyValue;

private static string BuildSecString(byte[] bytes)
    return new SecurityIdentifier(bytes,0).Value.ToString();

private static string BuildOctString(byte[] bytes)
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.Length; i++)
    return sb.ToString();

After load the property in directoryEntry ....

var usrId = (byte[])directoryEntry.Properties["objectSid"][0];
var objectID = (new SecurityIdentifier(usrId,0)).ToString();

Need Your Help

C++: Fastest method to check if all array elements are equal

c++ arrays

What is the fastest method to check if all elements of an array(preferable integer array) are equal. Till now I have been using the following code:

How do I access a network drive through the usual System.IO classes?

.net filesystems io network-drive

My software handles multiple operations on files, and I have now finished writing the related functions, using the System.IO classes.