ContextType.ApplicationDirectory中的AD LDS ValidateCredentials无法进行身份验证

Szy*_*iga 2 .net directoryservices adam active-directory lds

我正在尝试在我的MVC应用程序中使用AD LDS进行用户身份验证.我设法写了一些代码,允许我创建/编辑/删除用户和组,但我似乎无法验证它们.这是我的示例代码:

using( var context = new PrincipalContext(ContextType.ApplicationDirectory, "Lenovo_T61-LapT",
                                    "CN=Kontrahenci,DC=TestApp,DC=local"))
{
    var userName = "avg.joe";
    var email = "avg.joe@smwhr.us";
    var password = "123456";
    var user = new UserPrincipal(context)
    {
        Name = userName,
        EmailAddress = email
    };
    user.SetPassword(password);
    user.Save();
    if (context.ValidateCredentials(userName , password, ContextOptions.SimpleBind))
        Console.WriteLine("Hooray!");

    user.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这永远不会得到"Writeline"只给出一个密码或用户名不正确的错误.

我玩过ContextOptions但没有任何运气.

有任何想法吗?

Szy*_*iga 7

所以我找到了我在类似问题上发布的解决方案.

我做了什么,并且对我有用,就是在调用ValidateCredentials时我修改了用户名:

bool auth = context.ValidateCredentials(
                            String.Format("CN={0},CN=Kontrahenci,DC=TestApp,DC=loc",
                                          userName), 
                            password);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.