vto*_*ola 11 asp.net forms-authentication asp.net-authorization asp.net-mvc-4 asp.net-web-api
在ASP.NET中,FormsAuthenticationModule拦截任何HTTP 401,并返回HTTP 302重定向到登录页面.这对AJAX来说很痛苦,因为你要求json并在html中获取登录页面,但状态代码是HTTP 200.
在ASP.NET Web API中避免这种拦截的方法是什么?
在ASP.NET MVC4中,通过明确结束连接来防止此拦截非常容易:
public class MyMvcAuthFilter:AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest() && !filterContext.IsChildAction)
{
filterContext.Result = new HttpStatusCodeResult(401);
filterContext.HttpContext.Response.StatusCode = 401;
filterContext.HttpContext.Response.SuppressContent = true;
filterContext.HttpContext.Response.End();
}
else
base.HandleUnauthorizedRequest(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在ASP.NET Web API中我无法显式结束连接,因此即使我使用此代码,FormsAuthenticationModule也会拦截响应并将重定向发送到登录页面:
public class MyWebApiAuth: AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if(actionContext.Request.Headers.Any(h=>h.Key.Equals("X-Requested-With",StringComparison.OrdinalIgnoreCase)))
{
var xhr = actionContext.Request.Headers.Single(h => h.Key.Equals("X-Requested-With", StringComparison.OrdinalIgnoreCase)).Value.First();
if (xhr.Equals("XMLHttpRequest", StringComparison.OrdinalIgnoreCase))
{
// this does not work either
//throw new HttpResponseException(HttpStatusCode.Unauthorized);
actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
return;
}
}
base.HandleUnauthorizedRequest(actionContext);
}
}
Run Code Online (Sandbox Code Playgroud)
在ASP.NET Web API中避免此行为的方法是什么?我一直在看,我找不到办法.
问候.
PS:我不敢相信这是2012年,这个问题仍然存在.
如果有人有兴趣使用Authorize属性在ASP.NET MVC应用程序中处理相同的问题:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class Authorize2Attribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.Result = new HttpStatusCodeResult((int) HttpStatusCode.Forbidden);
}
else
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
}
base.HandleUnauthorizedRequest(filterContext);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样浏览器可以正确区分Forbidden和Unauthorized请求.
MVC 4 RC 的发行说明暗示这个问题自 Beta 版以来已得到解决 - 您使用的是哪个?
http://www.asp.net/whitepapers/mvc4-release-notes ASP.NET Web API 处理的未经授权的请求返回 401 Unauthroized:ASP.NET Web API 处理的未经授权的请求现在返回标准的 401 Unauthorized 响应,而不是重定向用户代理到登录表单,以便 Ajax 客户端可以处理响应。
查看 MVC 的源代码,似乎有通过 SuppressFormsAuthRedirectModule.cs 添加的功能
internal static bool GetEnabled(NameValueCollection appSettings)
{
// anything but "false" will return true, which is the default behavior
Run Code Online (Sandbox Code Playgroud)
所以看起来这是默认启用的,RC 应该可以解决您的问题,而不需要任何英雄行为...作为一个侧面,您似乎可以使用 AppSettings http://d.hatena.ne.jp/shiba-禁用这个新模块严/20120430/1335787815 :
<appSettings>
<Add Key = "webapi:EnableSuppressRedirect" value = "false" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
编辑(示例和说明)
我现在已经在GitHub上创建了此方法的示例。新的重定向抑制要求您使用两个正确的“授权”属性;控制器中的 MVC Web [System.Web.Mvc.Authorize] 和 Web API [System.Web.Http.Authorize] 和/或全局过滤器中的Link。
然而,这个例子确实指出了该方法的局限性。看来 web.config 中的“授权”节点将始终优先于 MVC 路由,例如这样的配置将覆盖您的规则并仍然重定向到登录:
<system.web>
<authentication mode="Forms">
</authentication>
<authorization>
<deny users="?"/> //will deny anonymous users to all routes including WebApi
</authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)
遗憾的是,使用 Location 元素为某些 url 路由打开此功能似乎不起作用,并且 WebApi 调用将继续被拦截并重定向到登录。
解决方案
对于 MVC 应用程序,我只是建议从 Web.Config 中删除配置并坚持使用代码中的全局过滤器和属性。
如果您必须使用 Web.Config 中的授权节点进行 MVC 或拥有混合 ASP.NET 和 WebApi 应用程序,那么@PilotBob - 在下面的评论中 - 发现子文件夹和多个 Web.Config 可用于拥有您的蛋糕和吃了它。
归档时间: |
|
查看次数: |
7384 次 |
最近记录: |