asp.net-mvc - 如何使用 ASP.Net MVC5 和 OWIN 返回 StatusCode 401

标签 asp.net-mvc angularjs asp.net-mvc-5 angular-ui-router owin

我想知道如何使用 ASP.Net MVC5、我的 IExceptionFilter 实现、Microsoft.Owin 和 ui-router ( https://github.com/angular-ui/ui-router)。

我的情况是:当权限出现问题时,我会向客户端发送响应,使用如下内容:

    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception == null)
        {
            return;
        }

        ...

        filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
        filterContext.Result = "Unathorized access";
    }

请注意:我已将 StatusCode 代码设置为 401。

为了从服务器获取 View 模板,我使用了请求 Action 方法的 $stateProvide。如你所知,ui-router 的核心部分 $stateProvide 可以处理 $stateChangeError。不幸的是,我的情况并非如此。

我有使用 Microsoft.Owin 的要求。由于某些原因,我的响应具有 StatusCode = 200(不是 401)并且具有“X-Responded-JSON” header :

X-Responded-JSON:{"status":401,"headers":{"location":"<URL>"}}

这是来自 http://kevin-junghans.blogspot.de/2013/12/returning-401-http-status-code-on.html 的引述“...OWIN 和 MVC 5 中的安全管道已更改,自定义属性不再返回 401 和 403 状态代码。而是返回 200 状态代码并在 header 中插入一些附加信息...”

基本上,这意味着不会触发 $stateChangeError。

有人知道如何解决这个问题吗? 谢谢。

最佳答案

解决方案实际上是在我添加到我的问题的链接中提供的。这是对我很有效的解决方案:

public class StartupAuth
{
    private static bool IsAjaxRequest(IOwinRequest request)
    {
        IReadableStringCollection query = request.Query;
        if ((query != null) && (query["X-Requested-With"] == "XMLHttpRequest"))
        {
            return true;
        }

        IHeaderDictionary headers = request.Headers;

        return ((headers != null) && (headers["X-Requested-With"] == "XMLHttpRequest"));
    }

    public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/login"),
            Provider = new CookieAuthenticationProvider
            {
                OnApplyRedirect = ctx =>
                {
                    if (!IsAjaxRequest(ctx.Request))
                    {
                        ctx.Response.Redirect(ctx.RedirectUri);
                    }
                }
            }
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        var configuration = (IConfiguration)DependencyResolver.Current.GetService(typeof(IConfiguration));
        app.UseFacebookAuthentication(configuration.FacebookAppId, configuration.FacebookAppSecret);
    }
}

现在 StatusCode = 401 并且触发了 $stateChangeError。

关于asp.net-mvc - 如何使用 ASP.Net MVC5 和 OWIN 返回 StatusCode 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27067659/

相关文章:

asp.net-mvc - 如何对 MVC 3 应用程序进行表单 POST 并获取反序列化类?

javascript - 使用 Angular js、Node js 服务器和 express js

asp.net-mvc-5 - 使用外部提供程序时如何记住MVC5中的登录名

c# - 将对象数组序列化为 Xxxxs 而不是 ArrayOfXxxx

c# - Azure Web 应用程序突然不再支持文化

angularjs - 类型错误:未定义不是函数 - Angular Animate

angularjs - Angular GET 请求陷入循环

asp.net - 如何在asp.net mvc5中创建动态角色

asp.net-mvc-5 - 无法从 ASP.NET MVC 5.1 上的 UrlHelper 调用 Action 方法

c# - foreach 循环内的 LINQ 表达式