gpt4 book ai didi

c# 检查用户是否是组的成员?

转载 作者:可可西里 更新时间:2023-11-01 07:50:21 25 4
gpt4 key购买 nike

我有一个代码可以用来检查用户是否是 AD 的成员,运行良好,

现在我想添加检查用户是否也是组成员的可能性!

我需要修改什么才能实现这一点,我做了一些工作,但失败了!

这是我的代码:

        //Authenticate a User Against the Directory
private bool Authenticate(string userName,string password, string domain)
{

if (userName == "" || password == "")
{
return false;
}

bool authentic = false;
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,userName, password);
object nativeObject = entry.NativeObject;
authentic = true;
}
catch (DirectoryServicesCOMException) { }
return authentic;
}

我想做成这样:

private bool Authenticate(string userName,string password, string domain, string group)

最佳答案

这在 Windows XP 或更早版本上不可用。

无论如何,为了检查组成员资格,您可以使用此代码:

bool IsInGroup(string user, string group)
{
using (var identity = new WindowsIdentity(user))
{
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(group);
}
}

关于c# 检查用户是否是组的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4366090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com