asp.net - HttpContext.Current.User.Identity.Name 为 null

标签 asp.net asp.net-mvc httpcontext

我尝试使用 HttpContext.Current.User.Identity.Name 从内部网络获取用户的 Windows 登录详细信息,但它是空的。

我尝试更改 Web.Config 中的身份验证模式,但没有感到高兴:

  <system.web>
    <authentication mode="Windows" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

我还听说,在项目属性中,您应该启用 Windows 身份验证并禁用匿名身份验证,如下所示:

enter image description here

但随后我在 Chrome 中遇到了重定向循环,并显示消息“此网页有重定向循环”。

我还检查了我的计算机上是否安装了 Windows 身份验证:

enter image description here

关于如何解决这个问题有什么想法吗?

非常感谢

最佳答案

当我有:

<authentication mode="Windows"/>
<identity impersonate="true/>

在 web.config 中,我通过以下方式获取当前用户:

string currUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

关于asp.net - HttpContext.Current.User.Identity.Name 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25080348/

相关文章:

asp.net-mvc - 如何从 HttpAuthenticationContext 获取自定义 header 值

c# - HttpContext的线程安全

asp.net - ASP.NET session 对象中的 Entity Framework 对象上下文?

c# - 缩小任何图像尺寸

asp.net - Itext,htmlworker.parseToList 上的访问被拒绝

c# - 如何确定网页是否已完全加载到网络浏览器中?

javascript - 通过 JavaScript 检索所有已安装打印机的列表

c# - OnCheckedChanged 事件未触发

asp.net-mvc - 尝试编写一些 razor 代码来在 Razor View 中定义 CSS 类,非常简单,但很困惑

c# - 在 LINQ 语句中使用部分类属性