Convert SID to Username in C#

user970444 picture user970444 · Sep 29, 2011 · Viewed 9k times · Source

In .net, I can create a NTAccount using domain and username, and get it's SID.

But I cannot convert the SID back to NTAccount using translate function.

new SecurityIdentifier(stringSid).Translate(typeof(NTAccount)).ToString();

And this two way conversion code has no problem running on Domain Controller.

Maybe some configuration wrong?

Answer

Alexander picture Alexander · Sep 29, 2011

SecurityIdentifier.Translate() method works only on domain accounts so perhaps your computer not attached to domain. To resolve local SIDs into account name you can use Win32 API function LookupAccountSid() look here for example.