扩展UserPrincipal以利用其内置属性...当我们重载FindByIdentity()方法时会遇到一个问题。
从微软在http://msdn.microsoft.com/en-us/library/bb384372%28VS.90%29.aspx的例子中(为了简短起见,排除了部分):
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("inetOrgPerson")]
public class InetOrgPerson : UserPrincipal {
// Implement the overloaded search method FindByIdentity
public static new InetOrgPerson FindByIdentity(PrincipalContext context,
string identityValue) {
return (InetOrgPerson)FindByIdentityWithType(context,
typeof(InetOrgPerson),
identityValue);
}
// Implement the overloaded search method FindByIdentity
public static new InetOrgPerson FindByIdentity(PrincipalContext context,
IdentityType identityType,
string identityValue) {
return (InetOrgPerson)FindByIdentityWithType(context,
typeof(InetOrgPerson),
identityType,
identityValue);
}
}如果我从MSDN示例中提取完全相同的代码并将其粘贴到我的应用程序中,它将无法工作。对InetOrgPerson.FindByIdentity()的调用返回null,如下所示:
if (null == InetOrgPerson.FindByIdentity(principalContext, UserName)) {
throw new Exception("bah");
}实际上,在InetOrgPerson.FindByIdentity()中,对FindByIdentityWithType()的调用返回null,如下所示:
if (null == FindByIdentityWithType(context, typeof(InetOrgPerson), identityType, identityValue) {
throw new Exception("bah");
}但是,调用:
FindByIdentityWithType(context, typeof(UserPrincipal), identityType, identityValue)给我想要的用户对象。但我不能使用它,因为它不能被转换成我需要返回的InetOrgPerson对象。
怎么回事?我希望微软自己的示例代码可以工作,但它不能工作,所以我试图基于该示例编写的代码自然也不能工作。有没有人做到这一点?
提前感谢!詹姆斯
发布于 2010-08-19 04:56:26
确保您要搜索的用户实际上属于类inetOrgPerson。
https://stackoverflow.com/questions/3515606
复制相似问题