如何获取asp.net Windows身份验证中的用户详细信息
发布时间:2020-05-23 04:51:47 所属栏目:asp.Net 来源:互联网
导读:我正在使用 Windows身份验证和访问用户名. IIdentity winId = HttpContext.Current.User.Identity;string name = winId.Name; 但我想获得其他详细信息,如用户全名和EmailID. 由于您在Windows网络上,因此您需要查询Active Directory以搜索用户,然后获取其属性,
|
我正在使用 Windows身份验证和访问用户名. IIdentity winId = HttpContext.Current.User.Identity; string name = winId.Name; 但我想获得其他详细信息,如用户全名和EmailID. 解决方法由于您在Windows网络上,因此您需要查询Active Directory以搜索用户,然后获取其属性,如电子邮件这是一个示例函数DisplayUser,它在Windows身份验证的网络上给出了一个IIdentity,找到用户的电子邮件: public static void Main() {
DisplayUser(WindowsIdentity.GetCurrent());
Console.ReadKey();
}
public static void DisplayUser(IIdentity id) {
WindowsIdentity winId = id as WindowsIdentity;
if (id == null) {
Console.WriteLine("Identity is not a windows identity");
return;
}
string userInQuestion = winId.Name.Split('')[1];
string myDomain = winId.Name.Split('')[0]; // this is the domain that the user is in
// the account that this program runs in should be authenticated in there
DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain);
DirectorySearcher adSearcher = new DirectorySearcher(entry);
adSearcher.SearchScope = SearchScope.Subtree;
adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))";
SearchResult userObject = adSearcher.FindOne();
if (userObject != null) {
string[] props = new string[] { "title","mail" };
foreach (string prop in props) {
Console.WriteLine("{0} : {1}",prop,userObject.Properties[prop][0]);
}
}
}
给出这个: 编辑:如果您收到“用户/密码错误” (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 用于Kendo网格模板中的循环
- asp.net – 为什么aspx文件返回404(“无法找到该页面”)
- asp.net – asp:UpdateProgress – 保留换行符
- asp.net-mvc – 单元测试我的控制器方法导致一个空的ViewNa
- asp.net core 2.0 – 多个项目解决方案docker文件
- asp.net-mvc – 允许Anonymous在asp.net mvc 3中调用某些操
- asp.net-mvc – 我如何编写一个MVC3 / 4应用程序,它既可以作
- asp.net – 如何更改.ASPX自动格式化设置(Visual Studio)
- asp.net-mvc – 缺少Visual Studio 2012 MVC本地数据库添加
- asp.net – MVC 4 Web API Action返回:类型vs HttpRespons
推荐文章
站长推荐
- asp.net – 如何查看Chrome开发者工具中发布到表
- asp.net – 在asp:超链接中分配声明值的问题 错
- asp.net – Jquery datepicker:验证日期mm/dd/y
- 当ASP.NET验证失败时,更改文本框的css类
- 将linkbutton设置为asp.net中asp:panel的默认按
- asp.net – 外键在VS2012 RC中未被识别
- asp.net-mvc-3 – Orchard CMS DataAnnotations
- asp.net-mvc – FormsAuthentication.SetAuthCoo
- asp.net确保javascript只加载一次
- asp.net-core – 如何自动增加MVC 6版本号?
热点阅读
