扩展 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 我需要返回的对象。

是什么赋予了?我希望Microsoft自己的示例代码可以正常工作,但这并不能,因此自然而然地,我试图根据示例编写的代码也无法正常工作。有人做了这项工作吗?

提前致谢!詹姆士

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top