简体   繁体   中英

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

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.

Used AD query - very simple:

DirectorySearcher ds = new DirectorySearcher();
ds.PropertiesToLoad.Add("employeeID");
ds.Filter = String.Format("(&(objectCategory=person)(sAMAccountName={0}))", loginName);

result = ds.FindOne();
if (result != null)
{
    personnelNumber = result.Properties["employeeID"][0].ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM