扩展 UserPrincipal; FindByIdentity() 失败

发布于 2024-09-15 03:15:14 字数 2243 浏览 2 评论 0原文

扩展 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() 的调用code> 返回 null,如下所示:

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

但是,调用:

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

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

什么给?我希望微软自己的示例代码能够工作,但事实并非如此,所以我尝试根据该示例编写的代码自然也无法工作。有人做过这个工作吗?

提前致谢! 詹姆斯

Extending UserPrincipal to take advantage of its built-in properties... running into an issue when we overload the FindByIdentity() method.

From Microsoft's example at http://msdn.microsoft.com/en-us/library/bb384372%28VS.90%29.aspx (parts excluded for brevity):

[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);
   } 
}

If I take the exact code from the MSDN example and paste it into my app, it doesn't work. The call to InetOrgPerson.FindByIdentity() returns null, as such:

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

In fact, from within InetOrgPerson.FindByIdentity(), the call to FindByIdentityWithType() returns null, as such:

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

However, the call:

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

gives me the user object I want. Except I can't use that, because it can't be cast to the InetOrgPerson object I need to return.

What gives? I'd expect Microsoft's own example code to work, but it doesn't, so naturally the code I'm trying to write based on the example isn't working, either. Has anyone made this work?

Thanks in advance!
James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2024-09-22 03:15:14

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

Make sure that the user you're searching for actually belongs to the class inetOrgPerson.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文