LogonUserIdentity.Name与User.Identity.Name

时间:2013-10-30 15:25:26

标签: asp.net c#-4.0 .net-4.0

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

通常,在Web应用程序中,如果我想确定该页面的当前用户是谁,那么我只需从 User.Identity.Name 检索其域登录。所以,当我点击F5在我的调试环境中运行我的解决方案(我的Web Forms 应用程序!)并发现User.Identity.Name为空时,我感到非常困惑。所以我随后用Google搜索“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>

除了EntityFramework的标签之外,我在web.config中的所有功能都是如此。

1 个答案:

答案 0 :(得分:4)

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

<windowsAuthentication enabled="true">
  <providers>
    <add value="Negotiate" />
    <add value="NTLM" />
  </providers>
</windowsAuthentication>

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

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