通过SID解析显示用户名的最佳方法是什么?

Den*_*s C 11 c# windows sid

我从注册表中读取了一个SID列表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList.

一个人怎么就解决了显示的用户名(例如DOMAIN\user,BUILT-IN\user)给予在C#中的SID字符串?

Den*_*s C 28

刚刚在pinvoke.net上找到它.

备用托管API:.Net 2.0中提供:

using System.Security.Principal;

// convert the user sid to a domain\name
string account = new SecurityIdentifier(stringSid).Translate(typeof(NTAccount)).ToString();
Run Code Online (Sandbox Code Playgroud)


Mit*_*eat 9

Win32 API函数LookupAccountSid()用于查找与SID对应的名称.

LookupAccountSid() 有以下签名:

BOOL LookupAccountSid(LPCTSTR lpSystemName, PSID Sid,LPTSTR Name, LPDWORD cbName,
                       LPTSTR ReferencedDomainName, LPDWORD cbReferencedDomainName,
                       PSID_NAME_USE peUse);
Run Code Online (Sandbox Code Playgroud)

MSDN 参考.

这是P/Invoke引用(带示例代码):http://www.pinvoke.net/default.aspx/advapi32.LookupAccountSid

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError = true)]
static extern bool LookupAccountSid (
  string lpSystemName,
  [MarshalAs(UnmanagedType.LPArray)] byte[] Sid,
  StringBuilder lpName,
  ref uint cchName,
  StringBuilder ReferencedDomainName,
  ref uint cchReferencedDomainName,
  out SID_NAME_USE peUse); 
Run Code Online (Sandbox Code Playgroud)