Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在授权失败时返回自定义视图或JSON,而不是显示用户名和密码对话框_Asp.net_Asp.net Mvc 4_Iis_Authorize Attribute - Fatal编程技术网

Asp.net 如何在授权失败时返回自定义视图或JSON,而不是显示用户名和密码对话框

Asp.net 如何在授权失败时返回自定义视图或JSON,而不是显示用户名和密码对话框,asp.net,asp.net-mvc-4,iis,authorize-attribute,Asp.net,Asp.net Mvc 4,Iis,Authorize Attribute,我正在开发asp.net mvc 4 web应用程序,并编写了以下自定义授权类:- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class CheckUserPermissionsAttribute : AuthorizeAttribute { public string Mo

我正在开发asp.net mvc 4 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;
                //base.HandleUnauthorizedRequest(filterContext);
            }
            return true;

           // base.OnAuthorization(filterContext);
        }
        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);
        }
    }
}
但我现在面临的唯一问题是,如果授权失败,则会提示用户输入用户名和密码,尽管我已经重写了HandleUnauthorizedRequest,以根据请求是否为AJAX返回视图或JSON。那么,您能告诉我们为什么在授权失败时会提示用户输入用户名和密码,而不是接收到_unauthorizedview或包含错误消息的JSON吗

但我现在面临的唯一问题是,如果授权失败 然后会提示用户输入用户名和密码, 虽然我已经覆盖了HandleUnauthorizedRequest以返回 根据请求是否为AJAX查看或JSON

这是因为在
HandleUnauthorizedRequest
方法中,您绝对总是在点击以下行:

base.HandleUnauthorizedRequest(filterContext);
你知道这条线是干什么的吗?它调用基方法。你知道基本方法是做什么的吗?它返回401状态代码。您知道在使用表单身份验证的ASP.NET应用程序中返回401状态响应代码时会发生什么吗?您将获得登录页面

所以,是的,如果您正在使用AJAX或其他东西,并且打算返回一些JSON或其他东西,请确保永远不会调用基本内容。顺便说一句,在您的
else
条件下,您似乎试图呈现一些
~/Views/Errors/\u Unauthorized.cshtml
视图,这显然是无用的,因为您还调用了基本方法,该方法将直接重定向到登录页面

我想在我回答的这个阶段,你已经知道该怎么做了:去掉你的
HandleUnauthorizedRequest
方法的最后一行,在这个方法中,你通过调用base方法将所有的努力都扔进了垃圾桶


如果您希望正确地执行操作并返回401状态代码,而不是获取登录页面,而是返回一些自定义JSON,那么您可以在响应对象上使用该属性。如果您使用的是没有此属性的.NET framework的某个旧版本,您可能会发现Phil Haack解释如何处理此情况的有用信息。

是的,我删除了base.HandleUnauthorizedRequest(filterContext);现在一切进展顺利。但是你能告诉我为什么返回401而不是真或假更好吗。。顺便说一下,我没有使用表单身份验证,而是我的应用程序使用windows身份验证。为什么返回401而不是true或false更好?因为HTTP协议规定,如果用户无权访问某些资源,服务器应返回401状态代码。那么,既然HTTP协议已经具备了处理这种情况所需的一切,为什么还要用正确或错误的东西来重新设计轮子呢?但是由于在我的ajax调用(ajax.actionlink&ajax.beginform)中,我希望OnSuccess脚本上的JSON显示未经授权的消息,所以如果我返回401,那么我需要调用onfailure脚本。所以现在一切都很顺利,我想保持现状?如果这可能会导致其他问题,您能给出建议吗?不,它可能不会导致其他问题。只是处理401状态代码的正确方法是订阅onerror回调。正如我在前面的评论中所说,当用户无权访问某些资源时,正确的做法是返回401状态码。但是您当然可以违反HTTP协议,在AJAX请求的
成功
回调中处理错误:-),但是在这种情况下,我将如何同时返回JSON和401。这是否可以从我的HandleUnauthorizedRequest方法返回两个值??