【发布时间】:2014-01-18 17:30:59
【问题描述】:
更新:
我通过一些相当简单的更改解决了这个问题,请参阅下面的自我回答。
原始问题:
我有一个同时使用 Windows 身份验证和 Forms 身份验证的 ASP.NET Web 应用程序。 Forms 身份验证在 Web.config 中定义为 authentication mode(请参阅下面的摘录)。在 IIS 7 中,在 Web 应用程序(AKA 虚拟目录)级别,匿名身份验证被禁用,而 Windows 身份验证被启用。
在 .NET 1.1 到 .NET 4.0 和 IIS6/7/7.5 成功通过 Windows auth 进行身份验证之后,但在通过 Forms auth 进行身份验证之前(创建表单身份验证票证/cookie),Global.Application_AuthenticateRequest() 看到 Request.IsAuthenticated 是 @ 987654326@。一旦Request.IsAuthenticated 变为true,System.Web.HttpContext.Current.User 的类型为System.Security.Principal.GenericPrincipal(而User.Identity 为System.Web.Security.FormsIdentity)
在 IIS7 服务器上安装 .NET 4.5 后,此行为发生了变化。未对 Web.config 文件进行任何更改,也未对 IIS 进行手动更改。我所做的唯一更改是安装 .NET 4.5。卸载 4.5 并重新安装 4.0 后,该行为恢复为“正常”。
我注意到的不同行为是,在通过 Windows 成功进行身份验证之后,但在通过表单进行身份验证之前(尚未创建表单身份验证票证),Application_AuthenticateRequest 现在显示Request.IsAuthenticated 是true。
此外,System.Web.HttpContext.Current.User.Identity 现在是 System.Security.Principal.WindowsIdentity(而不是 FormsIdentity)。
- 有人能解释一下为什么会有所不同吗?
- 是否有一个配置选项(如 web.config 更改或 IIS 设置)可以用来强制它以 4.0 方式工作? (这样就设置
Request.IsAuthenticated = true而言,Windows 身份验证不会胜过表单身份验证?)
我已经搜索了几个小时的 Msft 文档.. 他们所有关于混合 Windows 和 Forms 身份验证的信息似乎都已经过时了(2004 年左右),而关于 .NET 4.5 更改的细节在这个特定的部分相当稀疏地区。
web.config 的摘录:(是的,default.aspx 是故意的,在这种情况下我不使用 login.aspx,但它已经工作了 5 年以上,并且在所有以前的 .net 版本中都可以正常工作)。
<authentication mode="Forms">
<forms name=".ASPXAUTH" protection="All" timeout="200" loginUrl="default.aspx" defaultUrl="~/default.aspx" />
</authentication>
摘自 Global.asax.cs:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
// stuff for authenticated users
// prior to upgrading to .NET 4.5, this block was not hit until
// after the forms authentication ticket was created successfully
// (after validating user and password against app-specific database)
}
else
{
// stuff for unauthenticated users
// prior to upgrading to .NET 4.5, this block was hit
// AFTER windows auth passed but BEFORE forms auth passed
}
}
【问题讨论】:
-
我认为这个链接会对你有所帮助:stackoverflow.com/questions/12021863/…
-
您确定框架版本是唯一完成的更改吗?您的应用程序是在 IIS 7 的集成模式还是经典模式下运行。在您站点的应用程序池中查找托管管道。尝试更改模式。
-
它使用集成管道(安装4.5之前和之后)。唯一的变化是安装了 4.5。通过卸载 4.5 并重新安装 4.0 确认这一点:工作正常。然后我再次安装了 Framework v4.5,它又回到了这个不同的流程。
标签: asp.net authentication iis webforms .net-4.5