繁体   English   中英

扩展UserPrincipal; FindByIdentity()失败

[英]Extending UserPrincipal; FindByIdentity() fails

扩展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");
}

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

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

但是,电话:

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

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

是什么赋予了? 我希望微软自己的示例代码可以工作,但事实并非如此,因此我尝试根据示例编写的代码自然也无法正常工作。 有没有人做过这项工作?

提前致谢! 詹姆士

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM