如何获取asp.net Windows身份验证中的用户详细信息
发布时间:2020-12-15 19:26:11 所属栏目: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 – 如何在IIS7上全局设置enableVersionHeader为fal
- asp.net – 将数据传递到布局页面
- asp.net-mvc – mvc 4 beginform with route name
- asp.net – 主页和内容页面以及jquery
- Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之验证
- asp.net-mvc-4 – 请求太大
- asp.net-mvc-3 – 在区域注册中注册路由
- asp.net-mvc-3 – 用MVC构建一个大型项目软件
- asp.net core中使用log4net
- asp.net-core – 在VS 2015 RC中,编译保存不适用于ASP.NET
推荐文章
站长推荐
- asp.net – 使用Thinktecture Identity Server 2
- asp.net – 将SVN与Visual Studio 2003集成
- asp.net-mvc – 如何管理服务层中的事务?
- asp.net-mvc-3 – 您可以编辑CSHTML而无需再次构
- [ASP.NET MVC]如何设定Area底下的页面为起始页(网
- 使用Asp.net Mvc3 Razor视图方式扩展JQuery UI W
- asp.net – IControllerFactory’MyWebSite.WebU
- asp.net-mvc – 为什么Add View List Scaffoldin
- Asp.NET DropDownList在PostBack之后重置Selecte
- asp.net – 帮助捕获StackOverflowException与Wi
热点阅读