加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 当用户未通过身份验证时如何处理ajax请求?

发布时间:2020-12-15 18:36:04 所属栏目:asp.Net 来源:网络整理
导读:当用户未通过身份验证时,如何处理ajax请求? 有人进入页面,留下一个小时的空间,返回,在使用jQuery($ .post)的页面上添加评论。由于他没有认证,方法返回RedirectToRoute结果(重定向到登录页面)。你做什么?在客户端如何处理它,如何在控制器中处理? 解
当用户未通过身份验证时,如何处理ajax请求?

有人进入页面,留下一个小时的空间,返回,在使用jQuery($ .post)的页面上添加评论。由于他没有认证,方法返回RedirectToRoute结果(重定向到登录页面)。你做什么?在客户端如何处理它,如何在控制器中处理?

解决方法

编辑:

我以前写过上面的答案,现在我相信发送403是不正确的方法。 403具有略微不同的含义,它不应该被使用。这是使用401的更正属性。它仅与Http401Result中的附加context.HttpContext.Response.End()和不同的HTTP代码不同:

public class OptionalAuthorizeAttribute : AuthorizeAttribute
{
    private class Http401Result : ActionResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            // Set the response code to 401.
            context.HttpContext.Response.StatusCode = 401;
            context.HttpContext.Response.Write(CTRes.AuthorizationLostPleaseLogOutAndLogInAgainToContinue);
            context.HttpContext.Response.End();
        }
    }

    private readonly bool _authorize;

    public OptionalAuthorizeAttribute()
    {
        _authorize = true;
    }

    //OptionalAuthorize is turned on on base controller class,so it has to be turned off on some controller. 
    //That is why parameter is introduced.
    public OptionalAuthorizeAttribute(bool authorize)
    {
        _authorize = authorize;
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        //When authorize parameter is set to false,not authorization should be performed.
        if (!_authorize)
            return true;

        var result = base.AuthorizeCore(httpContext);

        return result;
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            //Ajax request doesn't return to login page,it just returns 401 error.
            filterContext.Result = new Http401Result();
        }
        else
            base.HandleUnauthorizedRequest(filterContext);
    }
}

旧答案:

虽然我喜欢在其他答案(我以前有一个想法)发布的想法,我需要代码示例。他们来了:

修改授权属性:

public class OptionalAuthorizeAttribute : AuthorizeAttribute
{
    private class Http403Result : ActionResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            // Set the response code to 403.
            context.HttpContext.Response.StatusCode = 403;
            context.HttpContext.Response.Write(CTRes.AuthorizationLostPleaseLogOutAndLogInAgainToContinue);
        }
    }

    private readonly bool _authorize;

    public OptionalAuthorizeAttribute()
    {
        _authorize = true;
    }

    //OptionalAuthorize is turned on on base controller class,it just returns 403 error.
            filterContext.Result = new Http403Result();
        }
        else
            base.HandleUnauthorizedRequest(filterContext);
    }
}

HandleUnauthorizedRequest被覆盖,所以在使用Ajax时返回Http403Result。 Http403Result将StatusCode更改为403,并向用户返回消息。在属性(授权参数)中有一些额外的逻辑,因为我打开了基本控制器中的[Authorize]并在某些页面中禁用它。

另一个重要的部分是在客户端对全局进行此响应。这是我在Site.Master中放置的:

<script type="text/javascript">
    $(document).ready(
        function() {
            $("body").ajaxError(
                function(e,request) {
                    if (request.status == 403) {
                        alert(request.responseText);
                        window.location = '/Logout';
                    }
                }
            );
        }
    );
</script>

我放置一个GLOBAL ajax错误处理程序,并且当$ .post失败并出现403错误时,响应消息将被提醒,并将用户重定向到注销页面。现在我不需要处理每个$ .post请求中的错误,因为它在全局处理。

为什么是403,而不是401? 401由MVC框架内部处理(这就是为什么重定向到登录页面是在授权失败后完成的)。

你怎么看待这件事?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读