首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过SID获取本地用户

通过SID获取本地用户
EN

Stack Overflow用户
提问于 2011-02-28 09:57:48
回答 2查看 3K关注 0票数 2

问:我想通过SID获得本地windows用户。

我找到了这个密码

代码语言:javascript
复制
[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中这样做:

代码语言:javascript
复制
Dim strURL As String = "WinNT://" + strComputerName + "/<SID=" + strSID + ">"
Dim de As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry(strURL)
de.Properties("whatever").Value.ToString()

然而,这是行不通的。任何人都知道我如何在不需要对所有用户进行循环的情况下执行这个(这需要首先将byte[]转换为string,然后比较大小写不敏感的很多字符串,这使得它变得很慢)。

EN

回答 2

Stack Overflow用户

发布于 2011-02-28 10:51:43

如果您使用的是.NET 3.5或更高版本,则可以使用以下代码(在向项目中添加对新System.DirectoryServices.AccountManagement命名空间的引用之后):

代码语言:javascript
复制
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");
}
票数 3
EN

Stack Overflow用户

发布于 2011-02-28 10:37:31

Win32有LookupAccountSid函数,它正是这样做的。

有关如何从PInvoke中使用域名,请参见VB.NET。在您的示例中,域名将是本地计算机名。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5140468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档