views:

80

answers:

1

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

+1  A: 

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

Justin R.
Yep, that was the problem. I didn't realize that the `DirectoryObjectClass` attribute I set tied the class to a class in AD. So now I understand that when I do a search through this class's `FindByIdentity`, I'm limiting my search to objects in AD of class 'inetOrgPerson', of which there are none in our AD. In my case, I want to set the `DirectoryObjectClass` to 'user'.That's actually pretty cool. Thanks!
James B