如何让asp.net Windows身份验证的用户详细信息

编程入门 行业动态 更新时间:2024-10-21 05:40:19
本文介绍了如何让asp Windows身份验证的用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Windows身份验证和访问用户名。

I am using windows Authentication and accessing user name as.

IIdentity winId = HttpContext.Current.User.Identity; string name = winId.Name;

但我想获得其他细节,如用户的全名和EMAILID。

but i want to get other details like User full name and EmailID.

推荐答案

既然你是Windows网络上,那么你需要查询Active Directory来搜索用户,然后得到它的属性,如电子邮件

Since you're on a windows network, then you need to query the Active directory to search for user and then get it's properties such as the email

下面是一个例子功能 DisplayUser 给定的的IIdentity 在Windows身份验证的网络上,找到用户的电子邮件:

Here is an example function DisplayUser that given an IIdentity on a windows authenticated network, finds the user's email:

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]); } } }

给出了这样的:

gives this:

编辑:如果你得到错误的用户/密码错误的code下运行,必须有访问权的用户域的帐户。如果你在asp中运行code,则该Web应用程序必须在与域访问凭据的应用程序池运行。请参见这里更多信息

If you get 'bad user/password errors' The account that the code runs under must have access the users domain. If you run code in asp then the web application must be run under an application pool with credentials with domain access. See here for more information

更多推荐

如何让asp.net Windows身份验证的用户详细信息

本文发布于:2023-05-26 10:44:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/355075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详细信息   身份验证   用户   asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!