我想获取当前登录用户的Employee-ID。这在一些.Net类中是否容易获得,或者我是否需要进行某种LDAP查询?
欢迎任何提示
答案 0 :(得分:6)
更简单 - 使用新的.NET 3.5 System.DirectoryServices.AccountManagement
功能。
有关详细信息,请参阅MSDN文章Managing Directory Security Principals in the .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变得轻而易举。
答案 1 :(得分:-1)
使用过的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();
}