C#,UserPrincipal.FindByIdentity总是抛出异常:“;服务器发送了一个referer“;或未知

本文关键字:未知 referer 一个 服务器 FindByIdentity UserPrincipal 抛出异常 | 更新日期: 2023-09-27 18:29:04

我花了一整天的时间却一无所获。我正在开发一个使用域用户/密码进行身份验证的web应用程序。遵循代码。前半部分是可以的,我可以得到输出:"用户和通行证是:True。"

然后,UserPrincipal.FindByIdentity抛出错误,要么是"服务器发送了一个referer"要么是"未知0x80005000"。我多次更改连接参数,如:LDAP://CN=Users,DC=sbi,DC=com

CN=用户,DC=sbi,DC=com

DC=sbi,DC=com

对于其中的每一个,前半部分代码都是正确的,我可以成功地对我的用户/通行证进行身份验证。但我无法获取userPrincipal。

我在谷歌上搜索了很多,但仍然一无所获,有人请帮忙。

    bool valid = false;
    using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "sbi.com", "LDAP://CN=Users,DC=sbi,DC=com"))
    {
        valid = context.ValidateCredentials(user, pass);
        System.Diagnostics.Debug.WriteLine("the user and pass is: " + valid.ToString());
    }
    //return;
    if (valid)
    {
        PrincipalContext context2 = new PrincipalContext(ContextType.Domain,
            "sbi.com", "CN=Users,DC=sbi,DC=com",
            ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing,
            user, pass);

        //System.Diagnostics.Debug.WriteLine("connected server:" + context2.ConnectedServer);
        UserPrincipal userInDomain = UserPrincipal.FindByIdentity(context2, user);
        if (userInDomain != null)
        {
            System.Diagnostics.Debug.WriteLine("user found: " + userInDomain.Name);
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("user not found");
        }
    }

C#,UserPrincipal.FindByIdentity总是抛出异常:“;服务器发送了一个referer“;或未知

我只是想明白了。如果我不指定容器字符串,它就会工作。也许是容器字符串出了问题。