Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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# 用户主体始终为空_C#_.net_Principalcontext - Fatal编程技术网

C# 用户主体始终为空

C# 用户主体始终为空,c#,.net,principalcontext,C#,.net,Principalcontext,我使用域域A下的用户User001登录,密码为Pass001 我使用这个代码 //var principalContext = new PrincipalContext( //ContextType.Domain, //"DomainA", //"User001", //"Pass001"); var principalContext = new PrincipalContext( ContextType.Domain, domain, use

我使用域域A下的用户User001登录,密码为Pass001

我使用这个代码

//var principalContext = new PrincipalContext(
    //ContextType.Domain,
    //"DomainA",
    //"User001",
    //"Pass001");

var principalContext = new PrincipalContext(
    ContextType.Domain,
domain, 
    userName,
    password);
var userPrincipal = new UserPrincipal(principalContext);
并且
userPrincipal
始终为空


如何修复它?

不知怎的,我发现这段代码运行得很好

 using (var context = new PrincipalContext(ContextType.Domain))
            {
                using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, userName))
                {
                    var uGroups = principal.GetGroups();
                    foreach (var group in uGroups)
                    {
                        Debug.WriteLine(group.DisplayName);
                    }
                }
            }

问题还不清楚,你能提供更多的代码和上下文吗?