Extending UserPrincipal; FindByIdentity() fails
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,
开发者_如何学Python 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
Make sure that the user you're searching for actually belongs to the class inetOrgPerson
.
精彩评论