UserPrincipal.FindByIdentity在.NET 4.5中失败(可能的错误?) [英] UserPrincipal.FindByIdentity fails in .NET 4.5 (possible bug?)

查看:95
本文介绍了UserPrincipal.FindByIdentity在.NET 4.5中失败(可能的错误?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些连接到Active Directory服务器并查找用户的C#代码。此功能的核心依赖于以下内容:

 private string pathSuffix =" DC = test-lab,DC = com" ;; 
private string domainName =" test-lab.com"
private string ipAddress =" [ip address]" ;;
private string targetAdministrator =" test-lab \\ [username]" ;;
private string targetPassword =" [password]" ;;

PrincipalContext context = new PrincipalContext(ContextType.Domain,ipAddress,pathSuffix,ContextOptions.SimpleBind,targetAdministrator,targetPassword);
UserPrincipal principal = UserPrincipal.FindByIdentity(context,name);

其中name是格式为"Firstname Lastname"的字符串。


这段代码已经运行了一段时间,但是在将我们的一台机器升级到.NET 4.5后,它立即停止工作。对FindByIdentity的调用抛出以下异常:

 [PrincipalOperationException:无法检索有关域的信息(1355)。] 
系统.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName,String domainName,String siteName,Int32 flags)+254
System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()+538
System.DirectoryServices.AccountManagement。 ADStoreCtx.get_DnsDomainName()+ 93
System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject,Object discriminant)+377
System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType,String urnScheme,String urnValue,DateTime referenceDate,Boolean useSidHistory)+1397
System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType,String urnScheme,String urnValue,DateTime referenceDate)+52
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context,Type principalType,Nullable`1 identityType,String identityValue,DateTime refDate)+157
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context,Type principalType, String identityValue)+73
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context,String identityValue)+25


<我没有很快宣布我在.NET中发现了一个实际的错误(我继承了很少知道Active Directory的代码),但似乎怀疑这个问题只发生在.NET 4.5下。我在四台机器上测试了代码,这些机器具有
相同的网络设置。三个运行4.0(在XP,win 7和Server 2008 R2下)完美运行;只有4.5(赢7)的那个失败了。项目本身在项目属性中声明为.NET 4.


有人昨天也发布了一个描述完全相同问题的Stackoverflow线程,但显然我是'我不允许发布链接。


这是.NET 4.5中的一个错误,还是我的代码做了一些不正确的代码,不再适用于4.5?

解决方案

有什么进展吗?我有完全相同的问题...&absp;


当我尝试从不同的域(在.NET 4.5下)检索用户时,会出现此问题。 .NET 4.0中的相同代码完美运行......


我的代码:

 UserPrincipal userPrincipal = new UserPrincipal(PrincipalContext) ; 
userPrincipal.Name =" *" ;;
PrincipalSearcher搜索者=新PrincipalSearcher(userPrincipal);
var allUsers = searcher.FindAll();

我和
StackOverflow。



I've inherited some C# code which connects to an Active Directory server and looks up users. The core of this functionality relies on the following:

private string pathSuffix = "DC=test-lab,DC=com";
private string domainName = "test-lab.com"
private string ipAddress  = "[ip address]";
private string targetAdministrator = "test-lab\\[username]";
private string targetPassword      = "[password]";

PrincipalContext context = new PrincipalContext(ContextType.Domain, ipAddress, pathSuffix, ContextOptions.SimpleBind, targetAdministrator, targetPassword);
UserPrincipal principal = UserPrincipal.FindByIdentity(context, name);

where name is a string in the format "Firstname Lastname".

This code has worked perfectly for some time, but upon upgrading one of our machines to .NET 4.5 it promptly stopped working. The call to FindByIdentity throws the following exception:

[PrincipalOperationException: Information about the domain could not be retrieved (1355).]
   System.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName, String domainName, String siteName, Int32 flags) +254
   System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo() +538
   System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsDomainName() +93
   System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant) +377
   System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory) +1397
   System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate) +52
   System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +157
   System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue) +73
   System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +25

I am not quick to declare that I've found an actual bug in .NET (I inherited this code with very little knowledge of Active Directory), but it seems suspect that the problem only occurs under .NET 4.5. I have tested the code on four machines, which have identical network settings. The three running 4.0 (under XP, win 7, and Server 2008 R2) work perfectly; only the one with 4.5 (on win 7) fails. The project itself is declared as .NET 4 in the project properties.

Someone also posted a Stackoverflow thread yesterday describing exactly the same problem, but apparently I'm not allowed to post links.

Is this a bug in .NET 4.5, or is my code doing something incorrect that no longer works in 4.5?

解决方案

Hi, any progress? I'm having the exact same issue... 

The issue occurs when I try to retrieve users from different domain (under .NET 4.5). The same code in .NET 4.0 works flawlessly...

My code:

UserPrincipal userPrincipal = new UserPrincipal(PrincipalContext);
userPrincipal.Name = "*";
PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal);
var allUsers = searcher.FindAll();

I'm having the same results as a guy having trouble on StackOverflow.


这篇关于UserPrincipal.FindByIdentity在.NET 4.5中失败(可能的错误?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆