Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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# UserPrincipal-UserPrincipal=UserPrincipal.FindByIdentity(ctx,Find.DisplayName)返回null_C#_.net - Fatal编程技术网

C# UserPrincipal-UserPrincipal=UserPrincipal.FindByIdentity(ctx,Find.DisplayName)返回null

C# UserPrincipal-UserPrincipal=UserPrincipal.FindByIdentity(ctx,Find.DisplayName)返回null,c#,.net,C#,.net,不知道为什么会发生这种情况,但当我运行这段代码时,它在一台服务器上工作,但在另一台服务器上不工作 两台服务器都返回正确的found.DisplayName,但是只有一台服务器返回USERPRINCIPAL的值,而另一台服务器返回空值 错误行: UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.DisplayName) returns null dynamic config

不知道为什么会发生这种情况,但当我运行这段代码时,它在一台服务器上工作,但在另一台服务器上不工作

两台服务器都返回正确的found.DisplayName,但是只有一台服务器返回USERPRINCIPAL的值,而另一台服务器返回空值

错误行:

       UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.DisplayName) returns null


        dynamic config = _getExpandoFromXml("config.xml");

        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, config.activeDirectory.sDomain, config.activeDirectory.sDefaultOU,config.mailServer.user, config.mailServer.pass);
        UserPrincipal user = new UserPrincipal(ctx);

        PrincipalSearcher search = new PrincipalSearcher(user);
        Console.WriteLine("before foreach");
        foreach (Principal found in search.FindAll())
        {
            try{
                if (found.DisplayName == null)
                {
                    Console.WriteLine("found.Dispalyname is null");
                }
                else
                {    
                    Console.Write("Dispalyname: ");
                    Console.WriteLine(found.DisplayName);
                }
                UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.DisplayName);
                Console.Write("looking for user: ");
                Console.WriteLine(found.DisplayName);
                Console.WriteLine("after findbyidentiy");
                if (oUserPrincipal == null)
                {
                    Console.WriteLine("oUserPrinciapal is null");
                }
                if (oUserPrincipal.LastPasswordSet == null)
                {
                    Console.WriteLine("lastpasswordset is null");
                }
                DateTime? dateOrNull = oUserPrincipal.LastPasswordSet;
                Console.WriteLine("after LastPasswordSet");

FindByIdentity只能搜索少数属性。这些是“枚举中包含的任何格式”

Name是一个有效的选项,但DisplayName未列出,因此您可能会得到DisplayName和Name恰好相同的结果,否则将失败

使用:

var oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.Name);

应该有用


还有一个允许您指定要搜索的属性。

在我的案例中,我发现问题在于它没有连接到AD服务器。如果我将鼠标悬停在
oPrincipalContext
上,其
ConnectedServer
属性显示它引发了类型为
System.DirectoryServices.DirectoryServicesCOMException
的异常。如果发生这种情况,则重新启动域控制器上的服务应该可以工作。我们发现这可能发生在高登录时间,因为我们的开发网络上只有一个DC


FindByIdentity需要SAMAccountName。DisplayName可以相同,也可以不同于用户的创建方式。尝试UserPrincipal.FindByIdentity(ctx,find.SamAccountName);行。明天试一试,我会告诉你进展如何。尼克,你知道这个问题吗?
var oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.SamAccountName);