【问题标题】:Get a user name from Virtual Machine [duplicate]从虚拟机获取用户名[重复]
【发布时间】:2017-04-24 12:13:30
【问题描述】:

我有一个小型 Web 应用程序,当前托管在虚拟机中。机器有Windows 8 + IIS 7。

作为应用程序的一部分,我需要获取访问这个该死的 Web 应用程序的人的用户名。但是每次我尝试获取当前用户时,我总是得到虚拟机名称。

如何正确获取访问该应用程序的机器名称。在虚拟机上?

【问题讨论】:

  • 你现在尝试了什么?
  • 这是 ASP.NET MVC 吗?当您对设计模式有特别的疑问时,您应该只使用model-view-controller 标签。当您对框架有疑问时,请使用asp.net-mvc 标记。
  • 我已经使用了“GetIPHost.HostName”、“Security.Principal.WindowsIdentity.GetCurrent()”和“Environment.UserName”。但是没有用..
  • user name of the people who visit this bloody web application。用户是否使用 Active Directory 登录?
  • 是的,德鲁·肯尼迪。所有用户都使用 Active Directory 登录..

标签: c# asp.net-mvc model-view-controller intranet


【解决方案1】:

如果用户通过 Active Directory 登录,这应该工作:

System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;

【讨论】:

  • 不幸的是,我得到“IIS APPPOOL\”作为用户名..
  • 听起来您可能在 IIS 中启用了匿名登录。确保已禁用它,并启用集成安全性。
  • 谢谢 Drew Kennedy.. 现在问题解决了..
【解决方案2】:

在控制器上,无论是使用 Windows 还是 Forms Auth,您都应该能够通过以下方式获取用户:

this.User.Identity.Name

或来自 HTTP 上下文:

HttpContext.Current.User.Identity.Name

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 2013-09-10
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多