0

嗨,我在如何创建自定义错误 401 消息方面遇到了障碍,我正在阅读一些关于如何在 C# 中创建自定义错误响应的教程,当我看到其中一些代码大部分都不起作用时有些看起来和理解起来很混乱。

这是我的代码

请注意,如果我正在使用的函数被授权,我使用 [Authorize] 属性返回。

[Authorize]     
        [HttpGet]
        public HttpResponseMessage gethistogram(string entity_name, string kpi_name, string chart_type, int unix_start, int unix_end, string language)
        {

            var result = _definitionRepository.histogram(entity_name,kpi_name,chart_type,unix_start,unix_end,language);
            //if (result == null)
            //{
            //    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, " Entity Name? Chart Type? KPI Name?, Language? Unix Start? or Unix End?");
            //}
            //return Request.CreateResponse(HttpStatusCode.OK, result);
            if (chart_type == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid chart Type to access data");
            }
            if (kpi_name == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid KPI name to access data");
            }
            if (entity_name == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid Entity name to access data");
            }
            if (kpi_name == null && chart_type == null && entity_name == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Required parameters missing to access data");
            }
            return Request.CreateResponse(HttpStatusCode.OK, result);

        }

我想更改默认错误响应

{
    "Message": "Authorization has been denied for this request."
}

进入

{
    "Message": "This request is unauthorized"
}

这是我的检查存储库

public List<Root> histogram(string entity_name, string kpi_name , string chart_type ,int unix_start, int unix_end, string language)
{
    var all = _db
        .kpi_definition
        //.Include("KPI")
        .Where(dl => entity_name == dl.entity_name && kpi_name == dl.kpi_name && chart_type == dl.chart_type && unix_start == dl.unix_start && unix_end == dl.unix_end && language == dl.language)
        .Select(dl => new Root
        {
            chart_type = new List<Chart>
            {
               new Chart { entity_name = dl.entity_name ,
                           entity_display_name = dl.entity_display_name,
                           kpi = new List<KPI>
                           {
                              new KPI {
                                       kpi_name = dl.kpi_name,
                                       kpi_display_name = dl.kpi_display_name,
                                       required = new List<Required>
                                       {
                                           new Required
                                           {
                                               //kpi_required = dl.kpi_required
                                           }
                                       },
                                       optional = new List<Optional>
                                       {
                                           new Optional
                                           {
                                               //kpi_optional = dl.kpi_optional
                                           }
                                       },
                                       objects = new List<Objects>
                                       {
                                           new Objects
                                           {
                                               field_name = new List<FieldName>
                                               {
                                                   new FieldName
                                                   {
                                                    entity_display_name = dl.entity_display_name,
                                                    type = "Select or Text",
                                                    @default = "default value(already selected)",
                                                    list = "",
                                                    ID = dl.ID
                                                   }
                                               }

                                           }
                                       }
                                      }
                           }
                         }
            }
        }).ToList();
    return all;
}
4

1 回答 1

1

我认为您可以创建一个自定义授权过滤器,将响应设置为 401 以及自定义消息。这是链接“如果 WebAPI 中的授权失败,如何返回自定义消息”,我相信这是您想要实现的。所以,简而言之,我们需要创建一个类,比如说 CustomAuthorize 继承 AuthorizeAttribute

public class CustomAuthorize : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(HttpActionContext 
       actionContext)
    {
        actionContext.Response = new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.Forbidden,
            Content = new StringContent("This request is unauthorized")
        };
    }
}

然后我们可以像这样在 gethistogram 方法之上使用这个属性而不是 Authorize 过滤器

    [CustomAuthorize]     
    [HttpGet]
    public HttpResponseMessage gethistogram(string entity_name, 
        string kpi_name, string chart_type, int unix_start, int 
        unix_end, string language)
    {

        var result = _definitionRepository.histogram(entity_name,kpi_name,chart_type,unix_start,unix_end,language);
        //if (result == null)
        //{
        //    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, " Entity Name? Chart Type? KPI Name?, Language? Unix Start? or Unix End?");
        //}
        //return Request.CreateResponse(HttpStatusCode.OK, result);
        if (chart_type == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid chart Type to access data");
        }
        if (kpi_name == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid KPI name to access data");
        }
        if (entity_name == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid Entity name to access data");
        }
        if (kpi_name == null && chart_type == null && entity_name == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Required parameters missing to access data");
        }
        return Request.CreateResponse(HttpStatusCode.OK, result);

    }
于 2020-01-08T00:17:21.987 回答