LogonUserIdentity.Name 与 User.Identity.Name

Jag*_*agd 4 asp.net .net-4.0 c#-4.0

我有一个在 4.0 框架上运行的 ASP.NET Web 窗体应用程序项目。我对 Web 应用程序非常熟悉,但对 Web 窗体应用程序不太熟悉。今天我遇到了他们两个之间的显着差异,我很困惑为什么这种差异甚至存在。

通常,在 Web 应用程序中,如果我想确定页面的当前用户是谁,那么我只需从User.Identity.Name检索他们的域登录信息。因此,当我在调试环境中按F5 运行我的解决方案(我的 Web窗体应用程序!)并发现 User.Identity.Name 为空时,我感到非常困惑。所以我然后在谷歌上搜索“User.Identity.Name 是空的”并遇到了一堵链接墙,这些链接主要是在他们没有禁用 IIS 中的匿名身份验证时遇到这个问题的人。当然,这不是我的问题(我认为),因为我只是在此处从 Visual Studio 2012 进行调试。

但是,我继续挖掘,最终在 HttpContext 上发现了一些不为空并返回用户域登录信息的晦涩属性。该属性是HttpContext.Current.Request.LogonUserIdentity.Name。这让我想到了我的问题......

问题:在我的 Web 窗体应用程序中,为什么 User.Identity.Name 为空而 HttpContext.Current.Request.LogonUserIdentity.Name 不是?

在此先感谢您的帮助!

编辑:忘记说明我在 web.config 中的内容(因为我确定如果我不这样做会被要求!)。

<system.web>
  <customErrors mode="Off" />
  <compilation debug="true" targetFramework="4.0" />
  <authentication mode="Windows"/>
</system.web>
<system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=10,chrome=1; IE=9,chrome=1; IE=8,chrome=1; IE=edge,chrome=1" />
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true" />
    <defaultDocument>
      <files>
        <add value="Home.aspx" />
      </files>
    </defaultDocument>
  </system.webServer>
Run Code Online (Sandbox Code Playgroud)

除了 EntityFramework 的标签之外,这几乎就是我在 web.config 中的所有内容。

Jag*_*agd 5

我自己找到了答案。IIS Express 的 applicationhost.config 设置为不允许 Windows 身份验证。我只是设置 enabled="true" 然后 User.Identity 不再是空的。

<windowsAuthentication enabled="true">
  <providers>
    <add value="Negotiate" />
    <add value="NTLM" />
  </providers>
</windowsAuthentication>
Run Code Online (Sandbox Code Playgroud)

编辑:
1)我为缺乏信息而道歉。我实际上有我在原始答案中修改的 XML 标记,但我显然忘记通过单击代码示例按钮将它们标记为代码,因此从未显示 XML 标记!他们现在应该是可见的。

2)我修改的applicationhost.config的位置在My Documents下的几个文件夹下。确切位置是:C:\Users[your user]\My Documents\IISExpress\config