扩展 UserPrincipal; FindByIdentity() 失败
扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您要搜索的用户确实属于
inetOrgPerson
类。Make sure that the user you're searching for actually belongs to the class
inetOrgPerson
.