首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有SPA(单页应用程序)的Brock Allen MembershipReboot微风应用程序授权重定向

具有SPA(单页应用程序)的Brock Allen MembershipReboot微风应用程序授权重定向
EN

Stack Overflow用户
提问于 2014-08-18 18:22:27
回答 2查看 256关注 0票数 0

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

代码语言:javascript
复制
[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();


                }

            }
        }
    }

当调用以下代码时,会发现授权不足:

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

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

你知道如何让代码运行,而不是加载到错误屏幕中吗?

EN

回答 2

Stack Overflow用户

发布于 2014-08-20 03:30:52

处理失败的promise并检查错误对象。您应该会发现其中的状态代码,它告诉您这是一个授权失败。现在,根据应用程序的需要进行重定向,而不是将错误报告给screen。

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

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

票数 0
EN

Stack Overflow用户

发布于 2014-08-23 01:00:53

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

代码很简单:

代码语言:javascript
复制
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错误,如下所示:

代码语言:javascript
复制
[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属性,以捕获任何未经授权的访问,如下所示:

代码语言:javascript
复制
    [System.Web.Http.HttpGet]
    [ValidateAntiForgeryToken]
    [Authorize]
    public string Metadata()
    {
    return _repository.Metadata;
    }

它可能很脏,但它确实起到了作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25361066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档