【问题标题】:handling my unauthorized requests , with an http 200 response + custom message处理我未经授权的请求,使用 http 200 响应 + 自定义消息
【发布时间】:2014-08-06 16:59:37
【问题描述】:

我在我的 asp.net mvc web 应用程序中有以下自定义授权类,我在我的操作方法之前调用它:-

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]

    public class CheckUserPermissionsAttribute : AuthorizeAttribute
    {

        public string Model { get; set; }
        public string Action { get; set; }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (!httpContext.Request.IsAuthenticated)
                return false;
            //code goes here................
            if (!repository.can(ADusername, Model, value)) // implement this method based on your tables and logic
            { return false; }
            return true;


        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {

                var viewResult = new JsonResult();
                viewResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                viewResult.Data = (new { IsSuccess = "Unauthorized", description = "Sorry, you do not have the required permission to perform this action." });
                filterContext.Result = viewResult;

            }
            else
            {
                var viewResult = new ViewResult();

                viewResult.ViewName = "~/Views/Errors/_Unauthorized.cshtml";

                filterContext.Result = viewResult;
            }

         //   base.HandleUnauthorizedRequest(filterContext);
        }
    }

我在我的操作方法之前调用这个自定义授权如下:-

    [CheckUserPermissions(Action = "Read", Model = "Accounts")]
public ActionResult Index(){

目前在上述代码中看到请求未授权时,我会根据请求类型(是否为Ajax请求)返回JSON或部分视图。

在我的代码中,我总是负责处理从 onsuccess 脚本中的自定义授权类返回的 json,如下所示:-

function addrecords(data) {
    if (data.IsSuccess == "Unauthorized") {

        jAlert(data.description, 'Unauthorized Access');
    }
    else        if (data.IsSuccess) {



            jAlert(data.description, 'Creation Confirmation');
    }

目前我的方法运行良好,但我开始考虑是否应该继续这样一个事实,即我不会为未经授权的请求重新调整 401 http 响应?而不是我返回一个 http 200 ,或者作为状态 =“未授权”的 json 对象或重定向到部分视图?

谁能给点建议?

谢谢。

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-5 authorize-attribute


    【解决方案1】:

    我以前是这样的:

     if (filterContext.HttpContext.Request.IsAjaxRequest())
     {
          filterContext.HttpContext.Response.StatusCode = 403;
          filterContext.Result = new JsonResult { Data = "LogOut" };
     }
     else
     {
          filterContext.Result = new RedirectResult("~/Home/Index");
     }
    

    在 jquery 中我签入通用 ajaxError:

    $(document).ajaxError(function(xhr, statusText, err){
    
        if(xhr.status == 403) {
    
          alert("Unathorized Request");
    
       }
    
    });
    

    或:

    $.ajaxSetup({
    
     error: function (x, e) {
    
         if (x.status == 403) {
    
             alert("Unauthorized Access");
    
    }
    
     });
    
    });
    

    在您的方法中,您必须检查每个 Ajax 调用是否成功以及即将发生的响应,但在这种方法中,在未经授权的情况下返回 403 代码将使 Ajax 调用失败并执行错误回调,我们使用它来编写通用错误处理程序对于 Ajax 并检查状态代码是否是我返回的内容,然后显示消息它是未经授权的请求。

    您可以查看详细信息:Asp.net mvc Check User is Logged In and authorized Before Access to Page

    【讨论】:

    • 所以对于未经授权的请求,您还返回 200 http 响应而不是 401,至少对于非 ajax 请求?
    • 但是我的方法有什么缺点?
    • 不,我返回 403,这将导致 ajax 调用失败,然后我用来捕获 ajax 的错误函数并检查返回的代码是否为 403 未经授权的访问
    • 但是我的方法有什么缺点?
    • 在您的方法中,您必须检查每个 ajax 调用成功的响应,但在这种方法中,在未经授权的情况下,返回 403 代码会使 ajax 调用失败,我们用它来编写一般错误ajax 处理程序并检查状态代码是否是我返回的内容,然后显示消息它是未经授权的请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多