繁体   English   中英

具有Windows身份验证的C#Web API-获取具有可读组名的本地Windows用户组-无广告

[英]C# Web API with Windows Authentication - Get Local Windows Users Groups with readable group name- No AD

我想列出本地Windows用户所属的所有组。 我可以使用以下命令在PowerShell中轻松完成此操作:

whoami /groups

gpresult /user <username> /r

到目前为止,我已经可以获取组了,但是我无法获取可读的名称,我得到的一切都是像S-1-1-0这样的值。 通过查看命令whoami /groups我可以看到SID与此值相对应并且正确。 为了获得易读的名称,我必须添加什么?

var groups = System.Security.Principal.WindowsIdentity.GetCurrent().Groups;

为了避免出现重复的问题标签的风险,我已经使用不同的AD方法new PrincipalContext(ContextType.Domain)来自System.DirectoryServices.AccountManagement new PrincipalContext(ContextType.Domain)找到了很多答案,但是由于此计算机未连接Active Directory,因此我在这里会遇到错误。 在这种情况下The server could not be contacted. - The LDAP server is unavailable. The server could not be contacted. - The LDAP server is unavailable. 由于相同的原因,我当然也不能做net user /domain <username> ,这里的错误将是System error 1355 has occurred.

您只需要使用ContentType.Machine创建PrincipalContext

var principalContext = new PrincipalContext(ContextType.Machine);
var groupPrincipal = new GroupPrincipal(principalContext);
var principalSearcher = new PrincipalSearcher(groupPrincipal);
var searchResult = principalSearcher.FindAll();

foreach (var principal in searchResult)
{
    System.Console.WriteLine(principal);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM