我可以从WindowsPrincipal获取Active Directory属性吗?
我想获取当前登录用户的Employee-ID。 这在一些.Net类中是否容易获得,还是需要进行某种LDAP查询?
欢迎任何提示
更简单 – 使用新的.NET 3.5 System.DirectoryServices.AccountManagement
function。
有关详细信息,请参阅MSDN文章管理.NET Framework 3.5中的目录安全性主体 。
PrincipalContext ctx = new PrincipalContext(ContextType.Domain. "YOURDOMAIN"); UserPrincipal user = UserPrincipal.FindByIdentity(ctx, loginName); if(user != null) { string empID = user.EmployeeId; }
新的强类型主要类使得与AD一起工作变得轻而易举。
二手AD查询 – 非常简单:
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(); }