weixin_33736649 2016-12-27 19:04 采纳率: 0%
浏览 66

Ajax授权失败

I am building MVC web application that for at least part of its data transfer relies on Ajax.

The controller action is

[RBAC]
[Authorize]
public string GetData(string inputdata)
{
   some code ...
   return jsondata;
}

The ajax call is

 $.ajax({
       dataType: "json",
       url: Url,
       data: { '_inputdata': selectedText },
       success: function (data)
       {
           response($.map(data,
              function(item, index) {
              return {
                   label: item.label,
                   value: item.value
               }
            }));
       },
      error: (function (jqXHR, textStatus, errorThrown, data) {
           ProcessFail(jqXHR, textStatus, errorThrown,  data);
        });
      })
  }); 

[RBAC] causes an authorization check to be done which is what I want.

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      ......
         filterContext.Result = new RedirectToRouteResult
              (new RouteValueDictionary { { "action", "Index" }, 
              { "controller", "Unauthorised" } , 
              { "Area", String.Empty }});
       .....
    } 

The problem is that I don't get anything back at the ajax except a failure. There is nothing that tells me that there was an authorization error.

Questions:

  1. Is it possible to get back information from an authorization failure into the ajax response. If so how?
  2. If the answer to 1. is no, should I be checking for this authorization before I make this call?

As always, any help appreciated.

  • 写回答

3条回答 默认 最新

  • weixin_33713350 2016-12-27 19:51
    关注

    Looks like you are using MVC rather than Web API, Web API should give you a nice JSON message by default.

    One option would be to check the status code of the response, this should give you a 401 if it is an authentication failure.

    Another would be to remove the [Authorize] and do a check inside of the method itself

    public string GetData(string inputdata)
    {
       if (User.Identity.IsAuthenticated) { 
          return  jsonData;
       } 
       return failureJson;
    }
    

    Note: I am sure there is a fancier way to do this but this should work

    评论

报告相同问题?

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大