byte array To Security Identifier - CSharp System

CSharp examples for System:Byte Array

Description

byte array To Security Identifier

Demo Code


using System.Text;
using System.Security.Principal;
using System.IO;// www .ja v  a2  s  .c  o m
using System;

public class Main{
        public static SecurityIdentifier ToSecurityIdentifier(this byte[] binarySid, bool bigEndianRid = false)
        {
            if(binarySid == null)
            {
                return null;
            }
            byte[] output = binarySid;
            if (bigEndianRid)
            {
                // Clone the binary SID so we do not perform byte spapping on the original value.
                byte[] binarySidCopy = (byte[])binarySid.Clone();
                int lastByteIndex = binarySidCopy.Length -1;
                // Convert RID from big endian to little endian (Reverse the order of the last 4 bytes)
                binarySidCopy.SwapBytes(lastByteIndex - 3, lastByteIndex);
                binarySidCopy.SwapBytes(lastByteIndex - 2, lastByteIndex - 1);
                output = binarySidCopy;
            }
            return new SecurityIdentifier(output, 0);
        }
        public static void SwapBytes(this byte[] bytes, int index1, int index2)
        {
            byte temp = bytes[index1];
            bytes[index1] = bytes[index2];
            bytes[index2] = temp;
        }
}

Related Tutorials