2011-02-28 110 views
2

問:我想通過SID獲得本地Windows用戶通過SID獲取本地用戶

我發現這個代碼

[ADSI]("WinNT://$Env:Computername/<SID=S-1-5-18>") 

這裏: http://www.eggheadcafe.com/software/aspnet/32882679/add-builtin-account-to-local-group-using-winnt-adsi-provider.aspx

我從中推導,我可以在(VB).NET中做到這一點:

Dim strURL As String = "WinNT://" + strComputerName + "/<SID=" + strSID + ">" 
Dim de As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry(strURL) 
de.Properties("whatever").Value.ToString() 

但是,這不起作用。 任何人都知道我怎麼能做到這一點沒有循環所有用戶(這需要先從字節[]轉換爲字符串,然後比較[不區分大小寫]很多字符串,這使得它很慢)。

回答

0

Win32具有LookupAccountSid函數,它正是這樣做的。

請參閱PInvoke瞭解如何使用VB.NET。在你的情況下,域名將是本地計算機名稱。

+0

不,的DirectoryServices,不會的PInvoke。此外,如果我願意,我可以在託管代碼中獲得NTaccount。 – 2011-02-28 11:54:55

3

如果你在.NET 3.5或更新版本,您可以使用此代碼(添加一個參照新System.DirectoryServices.AccountManagement命名空間到項目後):

using System.DirectoryServices.AccountManagement; 

// create a machine-context (local machine) 
PrincipalContext ctx = new PrincipalContext(ContextType.Machine); 

UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.Sid, <YourSidHere>); 

if (up != null) 
{ 
    Console.WriteLine("You've found: {0}", up.DisplayName); 
} 
else 
{ 
    Console.WriteLine("ERROR: no one found"); 
} 
+0

我在.NET 2.0上,此外,我已經有了獲得NTaccount的代碼。 – 2011-02-28 11:53:30

相關問題