[英]The best way to resolve display username by SID?
我从注册表中读取了一个SID列表, HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList
。
如果在C#中给出SID字符串,如何解析显示用户名(例如
DOMAIN\\user\u003c/code> ,
BUILT-IN\\user\u003c/code> )?
刚刚在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 参考
这是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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.