繁体   English   中英

重定向到Web Api授权中的默认值

[英]Redirect to default in Web Api Authorize

我的问题:会话期满后,用户仍然可以执行操作(搜索)。 操作结果是一些垃圾(不访问控制器)。 我不知道为什么; 我只想将用户重定向到登录页面。

我的计划是进行自定义Authorize并覆盖HandleUnauthorizedRequest(HttpActionContext)并将用户重定向到索引。 我不知道如何重定向到我的默认页面。

样例代码:

public class SessionTimeoutAttribute : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
    {
        base.HandleUnauthorizedRequest(actionContext);
        //redirect here
    }
}

您正在尝试将actionContext Response设置为Unauthorized http response。 这是一个示例。

public class SessionTimeoutAttribute: AuthorizeAttribute {
    protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) {

        base.HandleUnauthorizedRequest(actionContext);
        actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);

    }
}

希望这应该将用户重定向到您希望在CookieAuthenticationOptions定义的CookieAuthenticationOptions

编辑:老实说,如果您让用户以与输入普通页面相同的方式访问Web API,则这种定义Web API的目的。 您应该在客户端上确定来自Web api端点的响应是否success ,未unathorized等。您应该返回该值,而不是从Web api直接redirect

如果您确实要重定向,则可以尝试以下操作...

var response = actionContext.Request.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Location = new Uri("https://www.stackoverflow.com");
actionContext.Response = response;

同样,我看不到从Web API重定向的意义。 它仅应返回请求的数据/错误。 您应该在其他地方处理它。

我以为我可以直接从Web API重定向到网站...嗯,我不能。 原来,我不需要自定义授权,因为[授权]可以正确重定向。 授权需要时,我需要我的客户端重定向。 就我而言(角度1,5)是拦截器。

app.service('httpRedirectInterceptor', ['$window', function ($window) {
this.response = function (redirection) {
    if (typeof redirection.data === 'string') {
        if (redirection.data.indexOf instanceof Function &&
            redirection.data.indexOf('id="app-login-page"') != -1) {
            $window.location.pathname = "/Account/Login";
        }
    }
    return redirection;
};

this.request = function (req) {
    var elem = angular.element(document.body).find('div[ncg-request-verification-token]').attr('ncg-request-verification-token');
    req.headers['RequestVerificationToken'] = elem || "no request verification token";

    return req;
};}]);

并在app.config中

$httpProvider.interceptors.
push('httpRedirectInterceptor');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM