How to convert SID to String in .net

前端 未结 3 2027
囚心锁ツ
囚心锁ツ 2021-02-05 03:48

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         


        
相关标签:
3条回答
  • 2021-02-05 04:20

    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();
    
    0 讨论(0)
  • 2021-02-05 04:22

    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++)
        {
            sb.Append(bytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
    
    0 讨论(0)
  • 2021-02-05 04:22

    After load the property in directoryEntry ....

    var usrId = (byte[])directoryEntry.Properties["objectSid"][0];
    var objectID = (new SecurityIdentifier(usrId,0)).ToString();
    
    0 讨论(0)
提交回复
热议问题