Well sort of..
I have extended base class UserPrincipal with a few extra attributes. Works great and i'm digging it. However i have a problem i'm hoping someone can recreate. Problem is when I start off with a groupPrincipal and invoke the getmembers method, I get a run time error of unable to cast type UserPrincipal to NCSUserPrincipal.
My extended class is called NCSUserPrincipal
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain.local.whatever", "DC=domain,dc=local,dc=whatever", ContextOptions.Negotiate, "un", "pw");
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "domain users");
PrincipalSearchResult<Principal> members = group.GetMembers();
foreach (NCSUserPrincipal u in members) << crashes in this loop unable to cast UserPrincipal to NCSUserPrincipal.
{
}
If i start of with a NCSUserPrincipal it works great. Creating users with extended attributes also works fine.
NCSUserPrincipal ncs = new NCSUserPrincipal(ctx);
ncs.otherIpPhone = "3*";
PrincipalSearcher s = new PrincipalSearcher();
s.QueryFilter = ncs;
PrincipalSearchResult<Principal> users = s.FindAll();
foreach (NCSUserPrincipal u in users)
{
//do something here. works great.
}
Here is the ncsUserPrincipal Class
[
DirectoryRdnPrefix("CN")]
[
DirectoryObjectClass("user")]
public
class NCSUserPrincipal : UserPrincipal
{
// Inplement the constructor using the base class constructor.
public NCSUserPrincipal(PrincipalContext context)
:
base(context)
{
}
// Inplement the constructor with intialization parameters.
public NCSUserPrincipal(PrincipalContext context,
string samAccountName,
string password,
bool enabled)
:
base(context,
samAccountName,
password,
enabled)
{
}
// Create the otherIpPhone phone property.
[
DirectoryProperty("otherIpPhone")]
public string otherIpPhone
{
get
{
object[] result = this.ExtensionGet("otherIpPhone");
if (result != null)
{
return (string)result[0];
}
else
{
return null;
}
}
set
{
this.ExtensionSet("otherIpPhone", value);
}
}
public string office
{
get
{
object[] result = this.ExtensionGet("physicalDeliveryOfficeName");
if (result != null)
{
return (string)result[0];
}
else
{
return null;
}
}
set
{
this.ExtensionSet("physicalDeliveryOfficeName", value);
}
}
// Implement the overloaded search method FindByIdentity.
public static new NCSUserPrincipal FindByIdentity(PrincipalContext context,
string identityValue)
{
return (NCSUserPrincipal)FindByIdentityWithType(context,
typeof(NCSUserPrincipal),
identityValue);
}
//Implement the overloaded search method FindByIdentity.
public static new NCSUserPrincipal FindByIdentity(PrincipalContext context,
IdentityType identityType,
string identityValue)
{
return (NCSUserPrincipal)FindByIdentityWithType(context,
typeof(NCSUserPrincipal),
identityType,
identityValue);
}
}