2010-05-11 113 views

回答

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一起工作变得轻而易举。

+0

是的,这很简单。请注意它是UserPrincipal.EmployeeId 不是... ID 谢谢 – Graeme 2010-05-18 12:30:30

+1

谢谢马克,我正在寻找如何获得用户主体的Windows身份。在这里我意识到,如果我有域名,我可以做到这一点,而无需对我的Windows Identity对象做任何事情。 – Veverke 2015-09-20 14:41:22

-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(); 
} 
+0

结果从哪里来? “result = ds.FindOne();” – 2013-07-23 12:12:59

相关问题