Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get Active Directory attributes from the WindowsPrincipal?

I would like to get the Employee-ID for the currently logged in user. Is that easily available in some .Net class or do I need to do some sort of LDAP query?

Any tips welcome

like image 245
Graeme Avatar asked May 11 '10 08:05

Graeme


1 Answers

Even easier - use the new .NET 3.5 System.DirectoryServices.AccountManagement features.

See the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 for details.

PrincipalContext ctx = new PrincipalContext(ContextType.Domain. "YOURDOMAIN");

UserPrincipal user = UserPrincipal.FindByIdentity(ctx, loginName);

if(user != null)
{
   string empID = user.EmployeeId;
}

The new strongly typed principal classes make it a breeze to work with AD.

like image 55
marc_s Avatar answered Oct 05 '22 23:10

marc_s