【发布时间】:2010-05-11 08:05:11
【问题描述】:
我想获取当前登录用户的 Employee-ID。这在某些 .Net 类中很容易获得还是我需要进行某种 LDAP 查询?
欢迎任何提示
【问题讨论】:
标签: c# .net active-directory
我想获取当前登录用户的 Employee-ID。这在某些 .Net 类中很容易获得还是我需要进行某种 LDAP 查询?
欢迎任何提示
【问题讨论】:
标签: c# .net active-directory
更简单 - 使用新的 .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 变得轻而易举。
【讨论】:
使用 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();
}
【讨论】: