分享

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

 行走在理想边缘 2020-03-27
发布时间:2019-06-16 发布网站:脚本之家
脚本之家收集整理的这篇文章主要介绍了如何获取asp.net Windows身份验证中的用户详细信息脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Windows身份验证和访问用户名.
  1. IIdentity winId = HttpContext.Current.User.Identity;

  2. string name = winId.Name;

但我想获得其他详细信息,如用户全名和EmailID.

解决方法

由于您在Windows网络上,因此您需要查询Active Directory以搜索用户,然后获取其属性,如电子邮件

这是一个示例函数DisplayUser,它在Windows身份验证的网络上给出了一个IIdentity,找到用户的电子邮件:

  1. public static void Main() {

  2. DisplayUser(WindowsIdentity.GetCurrent());

  3. Console.ReadKey();

  4. }

  5. public static void DisplayUser(IIdentity id) {

  6. WindowsIdentity winId = id as WindowsIdentity;

  7. if (id == null) {

  8. Console.WriteLine("Identity is not a windows identity");

  9. return;

  10. }

  11. string userInQuestion = winId.Name.Split('\\')[1];

  12. string myDomain = winId.Name.Split('\\')[0]; // this is the domain that the user is in

  13. // the account that this program runs in should be authenticated in there

  14. DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain);

  15. DirectorySearcher adSearcher = new DirectorySearcher(entry);

  16. adSearcher.SearchScope = SearchScope.Subtree;

  17. adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))";

  18. SearchResult userObject = adSearcher.FindOne();

  19. if (userObject != null) {

  20. string[] props = new string[] { "title","mail" };

  21. foreach (string prop in props) {

  22. Console.WriteLine("{0} : {1}",prop,userObject.Properties[prop][0]);

  23. }

  24. }

  25. }

给出这个:

编辑:如果您收到“用户/密码错误”
代码运行的帐户必须具有用户域的访问权限.如果您在asp.net中运行代码,则Web应用程序必须在具有域访问权限的应用程序池下运行.有关详细信息,请参阅here

总结

以上是脚本之家为你收集整理的如何获取asp.net Windows身份验证中的用户详细信息全部内容,希望文章能够帮你解决如何获取asp.net Windows身份验证中的用户详细信息所遇到的程序开发问题。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多