【发布时间】:2017-04-07 21:01:40
【问题描述】:
我正在尝试从 Active Directory 中检索用户名。我发现这段代码可以尝试,但是它不能识别User.Identity.Name 的User 部分。我已经查看是否需要添加另一个引用或程序集,但是我没有看到任何东西。有没有更好的方法从 Active Directory 中获取用户名?
static string GetUserName(){
string name = "";
using (var context = new PrincipalContext(ContextType.Domain))
{
var usr = UserPrincipal.FindByIdentity(context, User.Identity.Name);
if (usr != null)
name = usr.DisplayName;
}
}
【问题讨论】:
标签: c# asp.net active-directory iprincipal