8

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

来自 Microsoft 在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");
}

事实上,从 insideInetOrgPerson.FindByIdentity()调用FindByIdentityWithType()返回 null,如下所示:

if (null == FindByIdentityWithType(context, typeof(InetOrgPerson), identityType, identityValue) {
    throw new Exception("bah");
}

但是,调用:

FindByIdentityWithType(context, typeof(UserPrincipal), identityType, identityValue)

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

是什么赋予了?我希望微软自己的示例代码能够工作,但它没有,所以我试图根据示例编写的代码自然也不起作用。有人做过这项工作吗?

提前致谢!詹姆士

4

1 回答 1

13

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

于 2010-08-18T20:56:26.037 回答