首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展UserPrincipal;FindByIdentity()失败

扩展UserPrincipal;FindByIdentity()失败
EN

Stack Overflow用户
提问于 2010-08-19 02:52:37
回答 1查看 5.9K关注 0票数 8

扩展UserPrincipal以利用其内置属性...当我们重载FindByIdentity()方法时会遇到一个问题。

从微软在http://msdn.microsoft.com/en-us/library/bb384372%28VS.90%29.aspx的例子中(为了简短起见,排除了部分):

代码语言:javascript
复制
[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,如下所示:

代码语言:javascript
复制
if (null == InetOrgPerson.FindByIdentity(principalContext, UserName)) {
     throw new Exception("bah");
}

实际上,在InetOrgPerson.FindByIdentity()中,对FindByIdentityWithType()的调用返回null,如下所示:

代码语言:javascript
复制
if (null == FindByIdentityWithType(context, typeof(InetOrgPerson), identityType, identityValue) {
    throw new Exception("bah");
}

但是,调用:

代码语言:javascript
复制
FindByIdentityWithType(context, typeof(UserPrincipal), identityType, identityValue)

给我想要的用户对象。但我不能使用它,因为它不能被转换成我需要返回的InetOrgPerson对象。

怎么回事?我希望微软自己的示例代码可以工作,但它不能工作,所以我试图基于该示例编写的代码自然也不能工作。有没有人做到这一点?

提前感谢!詹姆斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-19 04:56:26

确保您要搜索的用户实际上属于类inetOrgPerson

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3515606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档