Authorize attribute and jquery AJAX in asp.net MVC
2012-12-13 19:05
627 查看
在MVC中的控制器通过ajax的方式调用时权限的问题。
只写主要部分
按照错误方式处理
派生AuthorizeAttribute的类方法:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest()) //ajax调用方式
{
filterContext.Result = new ContentResult();
filterContext.HttpContext.Response.StatusCode = Convert.ToInt32(System.Net.HttpStatusCode.Forbidden);
}
else
{
filterContext.Result = new RedirectResult("/Account/LogOff");
}
}
客户端的代码
$.ajax({
type: "GET",
url: "/Test/Test",
async: false,
global: false,
data: {name:"sss"},
cache: false,
success: function (data, textStatus, jqXHR) {
$(outlookbarOpts.workspace).html(data);
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 403) { //此处判断StatusCode然后做跳转
window.location.href = "/Account/LogOff";
} else {
$(outlookbarOpts.workspace).html(jqXHR.responseText);
}
}
});
按照成功方式处理
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest()) //ajax调用方式
{
filterContext.Result = new JsonResult
{
Data = new { },
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
filterContext.Result = new RedirectResult("/Account/LogOff");
}
}
客户端的代码
$.ajax({
type: "GET",
url: "/Test/Test",
async: false,
global: false,
data: {name:"sss"},
cache: false,
success: function (data, textStatus, jqXHR) {
//在上面的Data中添加一个属性然后在此判断data的属性是否定义然后做出不同的处理方式跳转或者显示
});
只写主要部分
按照错误方式处理
派生AuthorizeAttribute的类方法:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest()) //ajax调用方式
{
filterContext.Result = new ContentResult();
filterContext.HttpContext.Response.StatusCode = Convert.ToInt32(System.Net.HttpStatusCode.Forbidden);
}
else
{
filterContext.Result = new RedirectResult("/Account/LogOff");
}
}
客户端的代码
$.ajax({
type: "GET",
url: "/Test/Test",
async: false,
global: false,
data: {name:"sss"},
cache: false,
success: function (data, textStatus, jqXHR) {
$(outlookbarOpts.workspace).html(data);
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 403) { //此处判断StatusCode然后做跳转
window.location.href = "/Account/LogOff";
} else {
$(outlookbarOpts.workspace).html(jqXHR.responseText);
}
}
});
按照成功方式处理
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest()) //ajax调用方式
{
filterContext.Result = new JsonResult
{
Data = new { },
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
filterContext.Result = new RedirectResult("/Account/LogOff");
}
}
客户端的代码
$.ajax({
type: "GET",
url: "/Test/Test",
async: false,
global: false,
data: {name:"sss"},
cache: false,
success: function (data, textStatus, jqXHR) {
//在上面的Data中添加一个属性然后在此判断data的属性是否定义然后做出不同的处理方式跳转或者显示
});
相关文章推荐
- Creating a Cascading Dropdown in ASP.net MVC 3 and jQuery (1)
- 热门的技术教程!ASP.NET MVC, JQuery, ASP.NET AJAX and ADO.NET Entity Framework
- Upload Files In ASP.NET Core 1.0 (Form POST And JQuery Ajax)
- Send and Receive JSON objects to Web Service Methods using jQuery AJAX in ASP.Net
- how to get all controller attribute action and parameter in asp.net mvc
- 热门的技术教程!ASP.NET MVC, JQuery, ASP.NET AJAX and ADO.NET Entity Framework
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- Using JQuery to perform Ajax calls in ASP.NET MVC
- JQuery文件上传插件ajaxFileUpload在Asp.net MVC中的使用
- Integrating jQuery UI and ASP.NET MVC
- 在ASP.net MVC里用Jquery的Ajax功能调用相关LinqtoSQL生成的类出现错误---循环引用
- How to use JSon data in mvc action and post form data use JQuery ajax
- Build Google IG like Ajax Start Page in 7 days using ASP.NET Ajax and .NET 3.0
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- Asp.net MVC中 model 验证方式 ajax +jquery
- Asp.net MVC中使用JQuery插件ajaxFileUpload上传文件
- 关于jQuery在Asp.Net Mvc 框架下Ajax文件上传的实现
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- Anti-Forgery Request Recipes For ASP.NET MVC And AJAX
- Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)