gpt4 book ai didi

c# - PrincipalContext Active Directory 验证容器中的用户

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:21 33 4
gpt4 key购买 nike

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain,"domain","CN=GroupName, DC = domainc, DC = local")) 
{
// validate the credentials
try
{
bool isValid = pc.ValidateCredentials("userName", "password");
}
catch (Exception e)
{

}
}

在 PrincipialContext 构造函数中,如果我只输入域名,我就可以验证用户。但是,如果我输入 CN="BadGroupNameDoesNotExist",它仍会将用户验证为 true。看来我为 CN 投入什么并不重要。

这是为什么呢?我是 Active Directory 的新手,我认为只有当他属于该组时,输入有效的 CN 才会有有效的凭据,但事实并非如此?

最佳答案

您可以尝试创建一个 utils 类并添加以下方法等。

using System.Security;
using System.DirectoryServices.AccountManagement;
public struct Credentials
{
public string Username;
public string Password;
}
public class Domain_Authentication
{
public Credentials Credentials;
public string Domain;
public Domain_Authentication(string Username, string Password, string SDomain)
{
Credentials.Username = Username;
Credentials.Password = Password;
Domain = SDomain;
}
public bool IsValid()
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain))
{
// validate the credentials
return pc.ValidateCredentials(Credentials.Username, Credentials.Password);
}
}
}

关于c# - PrincipalContext Active Directory 验证容器中的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40111944/

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