Created
July 12, 2011 16:55
-
-
Save fraga/1078411 to your computer and use it in GitHub Desktop.
C# console command line utility to get user SID AX 2012
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Diagnostics; | |
using System.DirectoryServices; | |
namespace GetUserSID | |
{ | |
/// <summary> | |
/// This class must work on a username and domainname, access active directory services | |
/// and retrieve the object SID | |
/// usage: | |
/// </summary> | |
public class GetUserSid | |
{ | |
/// <summary> | |
/// Main method to work on console | |
/// </summary> | |
/// <param name="args">Receives console argument</param> | |
static void Main(string[] args) | |
{ | |
if (args.Length == 0) | |
{ | |
GetUserSid.printHelp(); | |
} | |
else | |
{ | |
string userDomain = args[0]; | |
string SID = GetUserSid.GetSid(userDomain); | |
System.Console.WriteLine(SID); | |
} | |
} | |
/// <summary> | |
/// Prints the help, how to use it | |
/// </summary> | |
static public void printHelp() | |
{ | |
System.Console.WriteLine("Usage:"); | |
System.Console.WriteLine("Copy it to your windows system32 folder or a folder within %PATH%"); | |
System.Console.WriteLine("getusersid <domain\\username>"); | |
} | |
/// <summary> | |
/// Receives the login as DOMAIN\USER and works | |
/// | |
/// </summary> | |
/// <param name="strLogin">Domain\user active directory login</param> | |
/// <returns>returns the SID if found</returns> | |
public static string GetSid(string strLogin) | |
{ | |
string str = ""; | |
// Parse the string to check if domain name is present. | |
int idx = strLogin.IndexOf('\\'); | |
if (idx == -1) | |
{ | |
idx = strLogin.IndexOf('@'); | |
} | |
string strDomain; | |
string strName; | |
if (idx != -1) | |
{ | |
strDomain = strLogin.Substring(0, idx); | |
strName = strLogin.Substring(idx + 1); | |
} | |
else | |
{ | |
strDomain = Environment.MachineName; | |
strName = strLogin; | |
} | |
DirectoryEntry obDirEntry = null; | |
try | |
{ | |
Int64 iBigVal = 5; | |
Byte[] bigArr = BitConverter.GetBytes(iBigVal); | |
obDirEntry = new DirectoryEntry("WinNT://" + | |
strDomain + "/" + strName); | |
System.DirectoryServices.PropertyCollection | |
coll = obDirEntry.Properties; | |
object obVal = coll["objectSid"].Value; | |
if (null != obVal) | |
{ | |
str = GetUserSid.ConvertByteToStringSid((Byte[])obVal); | |
} | |
} | |
catch (Exception ex) | |
{ | |
str = ""; | |
System.Console.WriteLine(ex.Message); | |
} | |
return str; | |
} | |
/// <summary> | |
/// Converting raw bytes to string | |
/// </summary> | |
/// <param name="sidBytes">SID in bytes</param> | |
/// <returns>SID treated to string</returns> | |
private static string ConvertByteToStringSid(Byte[] sidBytes) | |
{ | |
StringBuilder strSid = new StringBuilder(); | |
strSid.Append("S-"); | |
try | |
{ | |
// Add SID revision. | |
strSid.Append(sidBytes[0].ToString()); | |
// Next six bytes are SID authority value. | |
if (sidBytes[6] != 0 || sidBytes[5] != 0) | |
{ | |
string strAuth = String.Format | |
("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}", | |
(Int16)sidBytes[1], | |
(Int16)sidBytes[2], | |
(Int16)sidBytes[3], | |
(Int16)sidBytes[4], | |
(Int16)sidBytes[5], | |
(Int16)sidBytes[6]); | |
strSid.Append("-"); | |
strSid.Append(strAuth); | |
} | |
else | |
{ | |
Int64 iVal = (Int32)(sidBytes[1]) + | |
(Int32)(sidBytes[2] << 8) + | |
(Int32)(sidBytes[3] << 16) + | |
(Int32)(sidBytes[4] << 24); | |
strSid.Append("-"); | |
strSid.Append(iVal.ToString()); | |
} | |
// Get sub authority count... | |
int iSubCount = Convert.ToInt32(sidBytes[7]); | |
int idxAuth = 0; | |
for (int i = 0; i < iSubCount; i++) | |
{ | |
idxAuth = 8 + i * 4; | |
UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth); | |
strSid.Append("-"); | |
strSid.Append(iSubAuth.ToString()); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Console.WriteLine(ex.Message); | |
return ""; | |
} | |
return strSid.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment