Well I thought I have written this post, but it was always in my mind to do and realized I haven't done it yet. Time has come to share my extended class to retrieve properties from Active Directory not listed under User Principal. I have used Directory Services which is compatible with earlier versions of .Net 3.5. The reason is UserPrincipal class has limitations. Eg: If you want Fax number then easy way to do this is using directory services.
Below is my extended class snippet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
namespace ExtPrincipalUser
{
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class ExtendedPrincipalUser : UserPrincipal
{
// Inplement the constructor using the base class constructor.
public ExtendedPrincipalUser(PrincipalContext ctx): base(ctx)
{
}
// Implement the constructor with initialization parameters.
public ExtendedPrincipalUser(PrincipalContext ctx, string samAccountName, string password, bool enabled)
: base(ctx, samAccountName, password, enabled)
{
}
[DirectoryProperty("facsimileTelephoneNumber")]
public string facsimileTelephoneNumber
{ get
{
if (ExtensionGet("facsimileTelephoneNumber").Length != 1)
return null;
return (string)ExtensionGet("facsimileTelephoneNumber")[0];
}
set
{
this.ExtensionSet("facsimileTelephoneNumber", value);
}
}
[DirectoryProperty("title")]
public string jobTitle
{
get
{
if (ExtensionGet("title").Length != 1)
return null;
return (string)ExtensionGet("title")[0];
}
set
{
this.ExtensionSet("title", value);
}
}
[DirectoryProperty("manager")]
public string manager
{
get
{
if (ExtensionGet("manager").Length != 1)
return string.Empty;
return (string)ExtensionGet("manager")[0];
}
set
{
this.ExtensionSet("manager", value);
}
}
[DirectoryProperty("facsimileTelephoneNumber")]
public string fax
{
get
{
if (ExtensionGet("facsimileTelephoneNumber").Length != 1)
return null;
return (string)ExtensionGet("facsimileTelephoneNumber")[0];
}
set
{
this.ExtensionSet("facsimileTelephoneNumber", value);
}
}
[DirectoryProperty("department")]
public string department
{
get
{
if (ExtensionGet("department").Length != 1)
return null;
return (string)ExtensionGet("department")[0];
}
set
{
this.ExtensionSet("department", value);
}
}
[DirectoryProperty("division")]
public string division
{
get
{
if (ExtensionGet("division").Length != 1)
return null;
return (string)ExtensionGet("division")[0];
}
set
{
this.ExtensionSet("division", value);
}
}
[DirectoryProperty("mobile")]
public string mobile
{
get
{
if (ExtensionGet("mobile").Length != 1)
return null;
return (string)ExtensionGet("mobile")[0];
}
set
{
this.ExtensionSet("mobile", value);
}
}
[DirectoryProperty("displayname")]
public string displayName
{
get
{
if (ExtensionGet("displayname").Length != 1)
return null;
return (string)ExtensionGet("displayname")[0];
}
set
{
this.ExtensionSet("displayname", value);
}
}
[DirectoryProperty("mail")]
public string mail
{
get
{
if (ExtensionGet("mail").Length != 1)
return null;
return (string)ExtensionGet("mail")[0];
}
set
{
this.ExtensionSet("mail", value);
}
}
// Implement the overloaded search method FindByIdentity.
public static new ExtendedPrincipalUser FindByIdentity(PrincipalContext context, string identityValue)
{
return (ExtendedPrincipalUser)FindByIdentityWithType(context, typeof(ExtendedPrincipalUser), identityValue);
}
// Implement the overloaded search method FindByIdentity.
public static new ExtendedPrincipalUser FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (ExtendedPrincipalUser)FindByIdentityWithType(context, typeof(ExtendedPrincipalUser), identityType, identityValue);
}
internal static ExtendedPrincipalUser FindByIdentity(PrincipalContext ctxMNGR)
{
throw new NotImplementedException();
}
}
}
No comments:
Post a Comment