public static class LdapHelper
{
// https://www.codeproject.com/Articles/3688/How-to-get-user-SID-using-DirectoryServices-classe
public static string ConvertByteToStringSid(byte[] sidBytes)
{
if (sidBytes == null || sidBytes.Length < 8 ||
sidBytes.Length > 68) // maximum 15 sub authorities
return string.Empty;
var span = new ReadOnlySpan<byte>(sidBytes);
var strSid = new StringBuilder("S-");
// Add SID revision.
strSid.Append(span[0]);
// Get sub authority count...
var subAuthoritiesLength = Convert.ToInt32(span[1]);
if (sidBytes.Length != 8 + subAuthoritiesLength * 4)
return string.Empty;
long identifierAuthority =
(((long)span[2]) << 40) +
(((long)span[3]) << 32) +
(((long)span[4]) << 24) +
(((long)span[5]) << 16) +
(((long)span[6]) << 8) +
span[7];
strSid.Append('-');
strSid.Append(identifierAuthority);
span = span[8..];
for (int i = 0; i < subAuthoritiesLength; i++, span = span[4..])
{
strSid.Append('-');
strSid.Append(BitConverter.ToUInt32(span.Slice(0, 4)));
}
return strSid.ToString();
}
}
ConvertByteToStringSid C#
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.