我有一个在 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>
这几乎是我在我的web.config中除了EntityFramework的标签之外
我自己找到了答案。IIS Express 的 applicationhost.config 设置为不允许 Windows 身份验证。我只是简单地设置了enabled="true",然后User.Identity不再为空。
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
编辑:
1)对于缺乏信息,我深表歉意。我实际上有我在原始答案中修改的 XML 标签,但我显然忘记通过单击"代码示例"按钮将它们标记为代码,因此从未显示 XML 标签!它们现在应该可见。
2)我修改的applicationhost.config的位置在我的文档下的几个文件夹中。确切的位置是:C:\Users[your user]\My Documents\IISExpress\config