属性授权 ajax 要求B. asp.net MVC 4

我有一种行动方法并向它发送 ajax, 像这样:


$.ajax/{
url: "/GetSearchCriteria",
type: "GET", //these is must
cache: false, //these is for IE
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
VehicleId : vehicleId
},
}/.done/function /data/ {
debugger;


$/'#myModal'/.modal/'show'/;

}/;


我定义了操作方法,如下所示:


[AjaxAuthorize]
[GET/"GetSearchCriteria"/]
public ActionResult GetSearchCriteria/VehicleSearchModel model/
{

return Json/model , JsonRequestBehavior.AllowGet/;
}


并授权方法 ajax 这样的要求:


public class AjaxAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest/AuthorizationContext context/
{
if /context.HttpContext.Request.IsAjaxRequest///
{
var urlHelper = new UrlHelper/context.RequestContext/;
context.HttpContext.Response.StatusCode = 403;
context.Result = new JsonResult
{
Data = new
{
Error = "NotAuthorized",
LogOnUrl = "/Login" //urlHelper.Action/"LogOn", "Account"/
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
base.HandleUnauthorizedRequest/context/;
}
}
}


然后这个代码 javacript:


$/function // {
$/document/.ajaxError/function /e, xhr/ {
debugger;
if /xhr.status == 403/ {
var response = $.parseJSON/xhr.responseText/;
window.location = response.LogOnUrl;
}
}/;
}/;


1/. 在大多数情况下,在大多数情况下,授权的这种属性不会下降。
2/. 即使他得到,用户也被重定向到逻辑页面,但退款 url 没有添加到K. url.
3/. 任何用户都可以登录/ 即使没有授权登录。 我只想要具有角色的用户 Customer 以不同的方式参与将其重定向到未授权的页面。

请告诉我如何做到这一点。
已邀请:

快网

赞同来自:

请确保您没有普通属性
[Authorize]

在水平
Controller

.

因为如果是这样,那么你的自定义
[AjaxAuthorize]

不会惊讶。

石油百科

赞同来自:

添加 AttributeUsage 到你的班级:


[AttributeUsage/AttributeTargets.Method | AttributeTargets.Class,
AllowMultiple = false, Inherited = true/]
public class AjaxAuthorizeAttribute : AuthorizeAttribute { ... }

要回复问题请先登录注册