代码之家  ›  专栏  ›  技术社区  ›  James King

扩展用户主体;findbyIdentity()失败

  •  8
  • James King  · 技术社区  · 14 年前

    延伸 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() 返回空值,例如:

    if (null == InetOrgPerson.FindByIdentity(principalContext, UserName)) {
         throw new Exception("bah");
    }
    

    事实上,从内部 inetOrgPerson.findbyIdentity()。 呼唤 FindByIdentityWithType() 返回空值,例如:

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

    但是,电话:

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

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

    给出了什么?我希望微软自己的示例代码可以工作,但它不能工作,所以我试图基于示例编写的代码自然也不能工作。有人做过这个工作吗?

    事先谢谢! 詹姆斯

    1 回复  |  直到 14 年前
        1
  •  13
  •   Justin Rusbatch    14 年前

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