2011-02-28 64 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

相关问题