UserPrincipal.FindByIdentityin.NET4.5Error(System.DirectoryServices.AccountManagement)在.NET4.5下测试我们的.NET4.0应用,我们遇到了UserPrincipal的FindByIdentity方法。以下代码在.NET4.0运行时运行时有效,但在.NET4.5下失败:[Test]publicvoidTestIsAccountLockedOut(){conststringactiveDirectoryServer="MyActiveDirectoryServer";conststringactiveDirectoryLogin="MyADAccount@MyDomain";conststringactiveDirectoryPassword="MyADAccountPassword";conststringuserAccountToTest="TestUser@MyDomain";conststringuserPasswordToTest="WRONGPASSWORD";varprincipalContext=newPrincipalContext(ContextType.Domain,activeDirectoryServer,activeDirectoryLogin,activeDirectoryPassword);varisAccountLockedOut=false;varisAuthenticated=principalContext.ValidateCredentials(userAccountToTest,userPasswordToTest,principalContext.Options);if(!isAuthenticated){//System.DirectoryServices.AccountManagement.PrincipalOperationException:无法检索有关域的信息(1355)。使用(varuser=UserPrincipal.FindByIdentity(principalContext,IdentityType.UserPrincipalName,userAccountToTest)){isAccountLockedOut=(user!=null)&&user.IsAccountLockedOut();}}Assert.False(isAuthenticated);Assert.False(isAccountLockedOut);}这是异常堆栈跟踪:System.DirectoryServices.AccountManagement.PrincipalOperationException:无法检索有关域的信息(1355)。在System.DirectoryServices.AccountManagement.Utils.GetDcName(StringcomputerName,StringdomainName,StringsiteName,Int32flags)在System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(类型principalType、字符串urnScheme、字符串urnValue、日期时间引用日期、布尔值useSidHistory)(类型原则ipalType,StringurnScheme,StringurnValue,DateTimereferenceDate)在System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext上下文,TypeprincipalType,Nullable`1identityType,StringidentityValue,DateTimerefDate)在Service.FindBydentyDirectoryManyages(Service.FindBydentyDirectoryManyages.PrincipalContext上下文,TypeprincipalType,IdentityTypeidentityType,StringidentityValue)atSystem.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContextcontext,IdentityTypeidentityType,StringidentityValue)还有其他人看到并解决了这个问题吗?如果没有,我们是否有更好的方法来检查ActiveDirectory帐户的IsAccountLockedOut状态?作为参考,我们所有的测试机器都在同一个子网中,具有不同的ActiveDirectory服务器,这些服务器运行WindowsServer2003、2008和2012,具有各种域功能模式(见下文)。该代码在运行.NET4.0的计算机上有效,但在运行.NET4.5的计算机上无效。我们运行代码的三台.NET机器是:-运行.NET4.0的Windows7-运行.NET4.5的WindowsVista-运行.NET4.5的WindowsServer2012我们尝试过的ActiveDirectory服务器是:-添加AD域功能Windows2003,模式设置为Windows2000Native–Windows2003,AD域功能模式设置为WindowsServer2003–Windows2008,Windows域功能模式设置为Windows2000Native–Windows域功能模式设置为WindowsServer2003Windows2008-Windows2008,Windows域功能模式设置为WindowsServer2008Windows2008-Windows2012,Windows域功能模式设置为Windows2012所有这些ActiveDirectory服务器都配置为一个简单的单一林,客户端计算机不属于域。它们不用于测试此行为的任何其他功能,并且不运行ActiveDirectory以外的任何东西。编辑-10/09/2012感谢所有回复的人。下面是演示该问题的C#命令行客户端,以及我们发现的短期解决方法,不需要我们更改有关ActiveDirectory和DNS配置的任何内容。似乎只有PrincipalContext的实例抛出一次异常。我们包括.NET4.0机器(Windows7)和.NET4.5机器(WindowsVista)的输出。使用系统;使用System.DirectoryServices.AccountManagement;namespaceADBug{classProgram{staticvoidMain(string[]args){conststringactiveDirectoryServer="MyActiveDirectoryServer";conststringactiveDirectoryLogin="MyADAccount";conststringactiveDirectoryPassword="MyADAccountPassword";conststringvalidUserAccount="TestUser@MyDomain.com";conststringunknownUserAccount="UnknownUser@MyDomain.com";varprincipalContext=newPrincipalContext(ContextType.Domain,activeDirectoryServer,activeDirectoryLogin,activeDirectoryPassword);//.NET4.0-首次尝试使用有效帐户找到用户//.NET4.5-首次尝试使用有效帐户失败并出现PrincipalOperationExceptionTestFindByIdentity(principalContext,validUserAccount,"ValidAccount-FirstAttempt");//使用有效帐户的第二次尝试找到用户TestFindByIdentity(principalContext,validUserAccount,"ValidAccount-SecondAttempt");//第一的尝试使用未知帐户找不到用户TestFindByIdentity(principalContext,unknownUserAccount,"UnknownAccount-FirstAttempt");//使用未知帐户的第二次尝试未找到用户(测试误报)TestFindByIdentity(principalContext,unknownUserAccount,"UnknownAccount-SecondAttempt");//使用有效帐户的后续尝试仍然找到用户TestFindByIdentity(principalContext,validUserAccount,"ValidAccount-ThirdAttempt");}privatestaticvoidTestFindByIdentity(PrincipalContextprincipalContext,stringuserAccountToTest,stringmessage){varexceptionThrown=false;varuserFound=false;尝试{使用(varuser=UserPrincipal.FindByIdentity(principalContext,IdentityType.UserPrincipalName,userAccountToTest)){userFound=(user!=null);}}catch(PrincipalOperationException){exceptionThrown=true;}Console.Out.WriteLine(message+"-抛出的异常={0}",exceptionThrown);有限公司nsole.Out.WriteLine(message+"-UserFound={1}",userAccountToTest,userFound);}}}.NET4.0输出有效帐户-第一次尝试-抛出异常=错误有效帐户-第一次尝试-找到用户=真实有效帐户-第二次尝试-抛出异常=错误有效帐户-第二次尝试-找到用户=真实未知帐户-第一次尝试-抛出的异常=错误的未知帐户-第一次尝试-找到的用户=错误的未知帐户-第二次尝试-抛出的异常=错误的未知帐户-第二次尝试-找到的用户=错误的有效帐户-第三次尝试-抛出的异常=错误的有效帐户-第三次尝试-找到用户=True.NET4.5输出有效帐户-第一次尝试-抛出异常=True有效帐户-第一次尝试-找到用户=错误有效帐户-第二次尝试-抛出异常=错误有效帐户-第二次尝试-找到用户=真正的未知帐户-第一次尝试-抛出的异常=虚假的未知帐户-第一次尝试-发现用户=虚假未知账户-第二次尝试-抛出异常=虚假未知账户-第二次尝试-发现用户=虚假有效账户-第三次尝试-抛出异常=虚假有效账户-第三次尝试-发现用户=真完全相同问题(跨源查询无法更新到4.5)——我认为这是一个错误,因为它破坏了现有的(4.0)代码,但是为了让它工作——看看其中一个(现在)失败的客户,我注意到有一堆DNS请求失败的SRV记录,形式为:_ldap._tcp.MYSERVER1.mydomain.com,INet,Srv_ldap._tcp.dc._msdcs.mydomain.com,INet,Srv修改我们的DNS服务器(使用DNS由故障客户端)为域中的一个DC的所有mydomain.com流量的转发区域确实解决了问题。使用nslookup,从之前(失败时)到现在(工作)的行为是在这些查询返回“不存在的域”之前,现在它们返回“*没有服务位置(SRV)记录可用于...”。失败点似乎是域意识到不存在,而不是缺少SRV记录。希望MS恢复此行为,但与此同时,如果您可以控制故障客户端的DNS,则创建DNS转发区域可能会有些运气。对于OP(以及帮助回复的任何其他人),我们(有)同样的问题。在我们的开发环境中,安装了VS2012,并且我们的应用程序在登录期间在运行时中断(如上所述的AD问题)。所以我的系统已经被擦除并转移到2010年,每次我读到一篇关于2012年有多糟糕的博客文章时都会泪流满面。所以我发现这篇文章归功于ScottHanselman。我在我的开发箱、Windows8Developer90DaysPreview和VS2012上安装了一个VM。启动并运行我们的应用程序,并立即遇到登录AD障碍。只需将我们的FindByIdentity包装在一个trycatch中,并强制它在第一次捕获后再次尝试-viola就可以了!所以感谢想出这个小把戏的人!所以这是一个小的修复,一个适用于本地开发的“hack”,应该不会影响生产,因为我们不会很快将4.5投入生产。但缺点是在本地,登录现在需要2分钟而不是几秒钟,当我们在2010年运行时:(我真的不知道我还能提供什么来帮助解决这个问题,但我想我还是分享了2美分点因为这仍然是一个主要问题。将.netframework从4.0升级到4.5后遇到了同样的问题我已经使用了.net4.5.1ugpradedframework。以上是.NET4.5错误(System.DirectoryServices.AccountManagement)分享,如果对你有用,需要了解更多C#学习教程,希望大家多多关注。本文收集自网络,不代表立场,如涉及侵权,请点击联系管理员删除权,如需转载请注明出处:
