我想获取当前登录用户的Employee-ID。在一些.Net类中很容易获得,还是需要做某种LDAP查询?我可以从WindowsPrincipal获取Active Directory属性吗?
任何提示欢迎
我想获取当前登录用户的Employee-ID。在一些.Net类中很容易获得,还是需要做某种LDAP查询?我可以从WindowsPrincipal获取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();
}
结果从哪里来? “result = ds.FindOne();” – 2013-07-23 12:12:59
是的,这很简单。请注意它是UserPrincipal.EmployeeId 不是... ID 谢谢 – Graeme 2010-05-18 12:30:30
谢谢马克,我正在寻找如何获得用户主体的Windows身份。在这里我意识到,如果我有域名,我可以做到这一点,而无需对我的Windows Identity对象做任何事情。 – Veverke 2015-09-20 14:41:22