Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过SID获取本地用户_C#_.net_Vb.net_Permissions_Directoryservices - Fatal编程技术网

C# 通过SID获取本地用户

C# 通过SID获取本地用户,c#,.net,vb.net,permissions,directoryservices,C#,.net,Vb.net,Permissions,Directoryservices,问题:我想通过SID获取本地windows用户: 我找到了这个密码 [ADSI]("WinNT://$Env:Computername/<SID=S-1-5-18>") [ADSI](“WinNT://$Env:Computername/”) 在这里: 我从中推断,我可以在(VB.NET)中使用以下内容: Dim strURL As String = "WinNT://" + strComputerName + "/<SID=" + strSID + ">" Dim

问题:我想通过SID获取本地windows用户

我找到了这个密码

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

我从中推断,我可以在(VB.NET)中使用以下内容:

Dim strURL As String = "WinNT://" + strComputerName + "/<SID=" + strSID + ">"
Dim de As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry(strURL)
de.Properties("whatever").Value.ToString()
Dim strURL As String=“WinNT://”+strComputerName+“/”
Dim de As DirectoryServices.DirectoryEntry=新的DirectoryServices.DirectoryEntry(strURL)
de.Properties(“任意”).Value.ToString()
然而,这不起作用。
任何人都知道我如何做到这一点,而不必对所有用户进行循环(这需要首先将字节[]转换为字符串,然后比较大量字符串(不区分大小写),这会使速度变慢)。

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


有关如何从VB.NET使用的信息,请参见。在您的情况下,域名将是本地计算机名。

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

使用System.DirectoryServices.AccountManagement;
//创建计算机上下文(本地计算机)
PrincipalContext ctx=新PrincipalContext(ContextType.Machine);
UserPrincipal up=UserPrincipal.FindByIdentity(ctx,IdentityType.Sid,);
如果(向上!=null)
{
WriteLine(“您已经找到:{0}”,up.DisplayName);
}
其他的
{
Console.WriteLine(“错误:未找到任何人”);
}

我使用的是.NET 2.0,此外,我已经有了获取NTaccount的代码。不,是DirectoryServices,不是pinvoke。此外,如果愿意,我可以在托管代码中获取NTaccount。
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");
}