Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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
C# Web API AuthorizeAttribute不返回自定义响应_C#_Asp.net Web Api_Asp.net Mvc 5 - Fatal编程技术网

C# Web API AuthorizeAttribute不返回自定义响应

C# Web API AuthorizeAttribute不返回自定义响应,c#,asp.net-web-api,asp.net-mvc-5,C#,Asp.net Web Api,Asp.net Mvc 5,当函数返回false时,如何使IsAuthorized返回自定义对象 在我的WebAPI项目中,我有一个类: public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext) { S

当函数返回false时,如何使IsAuthorized返回自定义对象

在我的WebAPI项目中,我有一个类:

public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
        {

            StandardWebAPIResponse invalidUserResponse = new StandardWebAPIResponse()
                    {
                        code = (int) Constants.ErrorCodes.InvalidCredentials,
                        data = "InvalidCredentials.",
                        StatusCode = HttpStatusCode.Unauthorized
                    };
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,
                        invalidUserResponse);
                    // if I set this to true I am getting 401 with my custom object
                    // otherwise it gives me default error message 
                    // {"Message":"Authorization has been denied for this request."}
                    return false;
        }
    }
由于某种原因,当我从IsAuthorized函数返回false时,它不会返回我的自定义invalidUserResponse对象。但如果我返回true,它就会返回它


如何解决此问题?

您应该覆盖使用
IsAuthorized
并刷新
响应的
授权
方法,或者强制刷新您的方法。在过滤器操作的地方填充响应更有意义。

我知道这个问题已经得到了回答,但我觉得有点错。我不认为针对未经授权请求的消息应该由
OnAuthorization
处理,而应该由
HandleUnauthorizedRequest
处理。我不确定将其放入授权中是否会导致任何重大问题,但可能是因为基类在
HandleUnauthorizedRequest中写入了您的响应,所以您得到的消息是true而不是false


这是一件微妙的事情,但授权的
指向
HandleUnauthorizedRequest
是有原因的。这主要是一种职责分离的方法,但是如果您不只是想发送错误消息,比如log bad request,那么您的
授权方法可能会变得拥挤。为了清晰起见,如果没有其他方法,您应该使用提供给您的方法。

是的,我同意。您需要实现从
AuthorizeAttribute
派生的自定义筛选器

protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
    base.HandleUnauthorizedRequest(actionContext);
    actionContext.Response = new HttpResponseMessage
    {
        StatusCode = HttpStatusCode.Unauthorized,
        Content = new ObjectContent(typeof(ErrorMessage),
        new ErrorMessage()
        {
            StatusCode = (int)HttpStatusCode.Unauthorized,
            Message = Constants.UnauthorisedErrorMessage,
            ErrorCode = Constants.UnauthorisedErrorCode
        }, new JsonMediaTypeFormatter())
    };
}

您应该重写OnAuthorization方法,该方法使用IsAuthorized并刷新响应,或者强制刷新您的方法。如何刷新响应,因此我找不到该函数/选项。是的,我选中了,他们将.End()从Http命名空间中移除。那么你应该覆盖OnAuthorization,在那里填充响应更有意义。非常感谢,它现在可以工作了。你可以把你的评论作为回答,然后我可以标记为回答。这可能对其他人也有帮助。谢谢,我会这样重新安排我的逻辑。