我已经将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();
}
}
}
}当调用以下代码时,会发现授权不足:
[System.Web.Http.HttpGet] [ValidateAntiForgeryToken] [Authorize] public string Metadata() { return _repository.Metadata; }
但是,重定向代码正在加载到Toast错误处理程序中,并显示为错误,并且重定向不起作用。
你知道如何让代码运行,而不是加载到错误屏幕中吗?
发布于 2014-08-20 03:30:52
处理失败的promise并检查错误对象。您应该会发现其中的状态代码,它告诉您这是一个授权失败。现在,根据应用程序的需要进行重定向,而不是将错误报告给screen。
我想我应该把这个“拦截器”放在我的"DataService"/"DataContext“抽象中,这样所有的Breeze调用都可以利用它。谁知道呢,你可能会用它来扩展EntityManager。我还没想太多。
当你让它工作时,你可能想和我们所有人分享这个“拦截器”。社区喜欢贡献。:-)
发布于 2014-08-23 01:00:53
我注意到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;
}它可能很脏,但它确实起到了作用。
https://stackoverflow.com/questions/25361066
复制相似问题