C# 使用http 200响应处理未经授权的请求+;自定义消息
我在asp.net mvc web应用程序中有以下自定义授权类,我在操作方法之前调用该类:-C# 使用http 200响应处理未经授权的请求+;自定义消息,c#,asp.net,asp.net-mvc,asp.net-mvc-5,authorize-attribute,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Authorize Attribute,我在asp.net mvc web应用程序中有以下自定义授权类,我在操作方法之前调用该类:- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class CheckUserPermissionsAttribute : AuthorizeAttribute { public str
[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(){
function addrecords(data) {
if (data.IsSuccess == "Unauthorized") {
jAlert(data.description, 'Unauthorized Access');
}
else if (data.IsSuccess) {
jAlert(data.description, 'Creation Confirmation');
}
目前,正如上面的代码所示,当请求未经授权时,我将根据请求类型返回JSON或部分视图(是否为Ajax请求)
在我的代码中,我始终负责处理onsuccess脚本中自定义授权类返回的json,如下所示:-
[CheckUserPermissions(Action = "Read", Model = "Accounts")]
public ActionResult Index(){
function addrecords(data) {
if (data.IsSuccess == "Unauthorized") {
jAlert(data.description, 'Unauthorized Access');
}
else if (data.IsSuccess) {
jAlert(data.description, 'Creation Confirmation');
}
目前,我的方法运行良好,但我开始考虑是否应该继续使用这样一个事实,即我没有为未经授权的请求重新调整401HTTP响应?相反,我返回一个HTTP200,作为status=“unauthrized”的json对象,或者重定向到部分视图
有人能给我建议吗
谢谢。我过去常这样做:
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.StatusCode = 403;
filterContext.Result = new JsonResult { Data = "LogOut" };
}
else
{
filterContext.Result = new RedirectResult("~/Home/Index");
}
在jquery中,我签入genericajaxError
:
$(document).ajaxError(function(xhr, statusText, err){
if(xhr.status == 403) {
alert("Unathorized Request");
}
});
或:
在您的方法中,您必须检查每一个Ajax调用是否成功以及将要发生的响应,但在这种方法中,在未经授权的情况下,返回403代码将使Ajax调用失败并执行错误回调,我们使用它为Ajax编写一个通用错误处理程序,并检查状态代码是否为返回的代码,然后显示消息,它是未经授权的请求
您可以看到详细信息:因此,对于未经授权的请求,您还返回200 http响应,而不是401响应,至少对于非ajax请求?但我的方法有什么缺点?没有,我返回403,这将使ajax调用失败,然后我使用捕获ajax的错误函数,并检查返回的代码是否为403 Unhorized Access。但是我的方法有什么缺点?在你的方法中,你必须检查每个ajax调用成功的响应是什么即将到来,但在这种方法中,在未优化的情况下,返回403代码将使ajax调用失败,我使用它来为ajax编写一个通用错误处理程序,并检查状态代码是否是我返回的,然后显示消息,它是未优化的请求