Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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# PrincipicalContext ActiveDirectory验证容器中的用户_C#_Validation_Ldap - Fatal编程技术网

C# PrincipicalContext ActiveDirectory验证容器中的用户

C# PrincipicalContext ActiveDirectory验证容器中的用户,c#,validation,ldap,C#,Validation,Ldap,在PrincipialContext构造函数中,如果我只输入域名,我就能够验证用户。但是,如果我输入CN=“BadGroupNameDoesNotExist”,它仍然会验证用户为true。看来我为CN投了什么并不重要 为什么呢?我是active directory新手,我认为只有当他属于该组时,输入有效的CN才会具有有效的凭据,但这似乎不是真的?您可以尝试创建一个utils类并添加以下方法等 using (PrincipalContext pc = new PrincipalContext(Co

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


为什么呢?我是active directory新手,我认为只有当他属于该组时,输入有效的CN才会具有有效的凭据,但这似乎不是真的?

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

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)
    {

    }
}

您的回答是正确的,
ValidateCredentials
不注意容器名称/OU

中的两个备注部分包括

ValidateCredentials方法绑定到构造函数中指定的服务器


没有提到正在使用的构造函数中指定的容器,正如您所发现的,它也没有被检查。

您不需要像现在这样通过CN,我将发布一些我目前正在做的事情,这些事情可以在ADH中验证用户。如果用户属于某个组,我如何让它返回true?您可以在using for中使用
GroupPrincipal
示例
GroupPrincipal group=GroupPrincipal.FindByIdentity(principalContext,“YourGroupName”)
然后您可以在group.Members属性中搜索,直到找到所需的主体。例如
foreach(group.Members中的主体prnc)
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);
        }
    }
}