【发布时间】:2008-12-19 03:32:31
【问题描述】:
我从注册表中读取了一个 SID 列表,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList。
在给定 C# 中的 SID 字符串的情况下,如何解析显示用户名(例如 DOMAIN\user、BUILT-IN\user)?
【问题讨论】:
我从注册表中读取了一个 SID 列表,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList。
在给定 C# 中的 SID 字符串的情况下,如何解析显示用户名(例如 DOMAIN\user、BUILT-IN\user)?
【问题讨论】:
刚刚在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();
【讨论】:
Win32 API 函数LookupAccountSid() 用于查找与 SID 对应的名称。
LookupAccountSid() 具有以下签名:
BOOL LookupAccountSid(LPCTSTR lpSystemName, PSID Sid,LPTSTR Name, LPDWORD cbName,
LPTSTR ReferencedDomainName, LPDWORD cbReferencedDomainName,
PSID_NAME_USE peUse);
MSDN Ref.
这是 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);
【讨论】: