【问题标题】:Brock Allen MembershipReboot with SPA(Single Page Application) Breeze Application Authorization RedirectBrock Allen MembershipReboot with SPA(Single Page Application) Breeze Application Authorization Redirect
【发布时间】:2014-08-18 10:22:27
【问题描述】:

我已将 MembershipReboot 与 Breeze SPA 应用程序集成,并且登录和授权按预期工作。在 BreezeController.cs 中,我添加了以下代码来捕获授权失败。

[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class AuthorizeAttribute : System.Web.Http.Filters.AuthorizationFilterAttribute
    {

        public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            base.OnAuthorization(actionContext);

            ////check authentication and return if not authorized
            if (actionContext != null)
            {
                if (!actionContext.RequestContext.Principal.Identity.IsAuthenticated)
                {
                    actionContext.Response = actionContext.ControllerContext.Request.CreateResponse(System.Net.HttpStatusCode.Redirect);
                    System.Web.HttpContext.Current.Server.ClearError();
                    System.Web.HttpContext.Current.Response.Redirect("/UserAccount/Home",true);
                    //***********
                    //REDIRECT BEING CAUGHT BY ANGULAR ERROR HANDLER!!!
                    //**********
                    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();


                }

            }
        }
    }

调用下面的代码时发现缺少Authorization:

[System.Web.Http.HttpGet] [ValidateAntiForgeryToken] [Authorize] public string Metadata() { return _repository.Metadata; }

但是,重定向代码被加载到 Toast 错误处理程序中并显示为错误,并且重定向不起作用。

有什么想法可以让代码运行而不是加载到错误屏幕中?

【问题讨论】:

    标签: authorization breeze membershipreboot


    【解决方案1】:

    处理失败的承诺并检查错误对象。您应该在那里找到状态代码,告诉您这是授权失败。现在根据您的应用进行适当的重定向,而不是将错误报告到屏幕。

    我想我会将这个“拦截器”放在我的“DataService”/“DataContext”抽象中,以便所有 Breeze 调用都使用它。谁知道呢,你可能会用它来扩展 EntityManager。没想太多。

    当你让它工作时,你可能想与我们所有人分享这个“拦截器”。社区喜欢贡献。 :-)

    【讨论】:

    • 谢谢。我认为抓住 BreezeController.cs 中的错误将是最好的地方,因为 Authorize 和 AntiForgery 检查在那里。我会调查你的想法。我没有想到失败的承诺,因为其中有很多。
    【解决方案2】:

    我注意到 app/config.exceptionHandler.js 正在捕获我创建日志错误的所有错误。我已查找异常 401(未经授权的访问),如果发现则调用我的登录模块。

    代码很简单:

    var app = angular.module('app');
    
    // Configure by setting an optional string value for appErrorPrefix.
    // Accessible via config.appErrorPrefix (via config value).
    
    app.config(['$provide', function ($provide) {
        $provide.decorator('$exceptionHandler',
            ['$delegate', 'config', 'logger', extendExceptionHandler]);
    }]);
    
    // Extend the $exceptionHandler service to also display a toast.
    function extendExceptionHandler($delegate, config, logger) {
        var appErrorPrefix = config.appErrorPrefix;
        var logError = logger.getLogFn('app', 'error');
        return function (exception, cause) {
            $delegate(exception, cause);
            if (exception.status == 401) {
                window.location.href = "/UserAccount/Home";
            }
            if (exception.message == "undefined") { return; }
            if (appErrorPrefix && exception.message.indexOf(appErrorPrefix) === 0) { return; }
    
            var errorData = { exception: exception, cause: cause };
            var msg = appErrorPrefix + exception.message;
            logError(msg, errorData, true);
        };
    }
    

    })();

    修改了 BreezeController.cs 以生成 401 错误,如下所示:

    [AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class AuthorizeAttribute : System.Web.Http.Filters.AuthorizationFilterAttribute
    {
        public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            base.OnAuthorization(actionContext);
    
            ////check authentication and return if not authorized
            if (actionContext != null)
            {
                if (!actionContext.RequestContext.Principal.Identity.IsAuthenticated)
                {
                actionContext.Response = 
                    new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized) { RequestMessage = actionContext.ControllerContext.Request };
                }
            }
        }
    }
    

    控制器中的条目被赋予属性 [Authorize] 以捕获任何未经授权的访问,如下所示:

        [System.Web.Http.HttpGet]
        [ValidateAntiForgeryToken]
        [Authorize]
        public string Metadata()
        {
        return _repository.Metadata;
        }
    

    它可能很脏,但它可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-07
      • 2013-03-07
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多