2008-12-19 63 views

回答

9

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); 
+0

是否有任何其他方式做到这一点不使用P/Invoke在C#中? – 2008-12-19 03:57:21

+0

@ DennisC你可以做到没有P/Invoke。请参阅我的答案:http://stackoverflow.com/questions/7593005/convert-sid-to-username-in-c-sharp/7593200 – 2017-03-21 02:10:17

26

刚刚发现它的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(); 
相关问题