在Swagger上为.net Web API调用添加自定义标头字段的最佳做法是什么? [英] What are the best practices in adding custom header fields for a .net web api call on Swagger?

查看:302
本文介绍了在Swagger上为.net Web API调用添加自定义标头字段的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到很多在线方法,但其中大多数都是凌乱的,对我而言,我使用的是这两种方法

I can see a lot of ways to do it online but most of them are messy, for me I was using these two ways

  • 使用范围,我为移动设备做了一个,为网站做了另一个

  • Using scopes, I did one for mobile and another one for the website

var webScope = apiDescription.ActionDescriptor.GetFilterPipeline()
   .Select(filterInfo => filterInfo.Instance)
   .OfType<WebAuthorize>()
   .SelectMany(attr => attr.Roles.Split(','))
   .Distinct();

var mobileScope = apiDescription.ActionDescriptor.GetFilterPipeline()
    .Select(filterInfo => filterInfo.Instance)
    .OfType<MobileAuthorize>()
   .SelectMany(attr => attr.Roles.Split(','))
   .Distinct();

之所以起作用,是因为我有两种不同的方式来授权api调用,如您所见,我具有移动授权和网络授权,因此我的api调用看起来像这样:

And it worked because I had two different ways in authorizing the api calls, as you can see I had a Mobile Authorize and a Web Authorize so my api calls would look something like this:

[HttpGet]
[Route("something")]
[WebAuthorize(Code = PermissionCode, Type =PermissionType)]
public async Task<Dto> Getsomething()
{
    return await unitOfWork.GetService<ISomething>().GetSomething();
}

使用范围时遇到的问题是,所有具有网络授权的调用都将共享相同的标头,因此对于特殊调用,我使用了另一种添加自定义标头的方式.

Issues I face when using scopes is that all calls that have web authorize will share the same headers so for the special calls I used another way to add custom headers.

使用apiDescription.RelativePath,我将检查它是否相对路径等于我要添加该自定义标头的api调用,例如:

Using apiDescription.RelativePath, and I will check it if the relative path is equal to the api call I want to add that custom header, example:

[HttpPost]
[Route("rename")]
[InHouseAuthorize(Code = PermissionCode, Type =PermissionType)]
public async Task<HttpResponseMessage> RenameDevice()
{
    HttpRequestMessage request = Request ?? new HttpRequestMessage();
    String deviceName = request.Headers.GetValues("deviceName").FirstOrDefault();
    String deviceGuid = request.Headers.GetValues("deviceGuid").FirstOrDefault();
    await unitOfWork.GetService<IDeviceService>().RenameDevice(deviceGuid, deviceName);
    await unitOfWork.Commit();
    return new HttpResponseMessage(HttpStatusCode.OK);
}

然后我将以下内容添加到AddRequiredHeaderParameter.cs中

And then I would add to the AddRequiredHeaderParameter.cs the following

    if (apiDescription.RelativePath.Contains("device/rename"))
    {
        operation.parameters.Add(new Parameter
        {
            name = "deviceGuid",
            @in = "header",
            description = "Add the Device Guid",
            type = "string",
            required = false
        });
        operation.parameters.Add(new Parameter
        {
            name = "DeviceName",
            @in = "header",
            description = "Add the Device Name",
            type = "string",
            required = false
        });
    }

起初这很方便且足够好修复,但是由于我添加了许多需要自定义标头的调用,因此事情变得很难看,如果相同的URL具有Get和Post,那么它甚至会变得更丑.

At first this was convenient and good enough fix but things are turning ugly as I'm adding a lot of calls that need custom headers and if the same URL have a Get and Post then it will even get uglier.

我正在寻找解决此问题的最佳方法.

I am searching for the best way to deal with this issue.

推荐答案

可以将[F​​romHeader]属性用于应在自定义标头中发送的网络方法参数(或Model类中的属性).像这样的东西:

It's possible to use attribute [FromHeader] for web methods parameters (or properties in a Model class) which should be sent in custom headers. Something like this:

[HttpGet]
public ActionResult Products([FromHeader(Name = "User-Identity")]string userIdentity)

对我来说,这似乎是最简单的解决方案.至少对于ASP.NET Core 2.1和Swashbuckle.AspNetCore 2.5.0来说,它可以正常工作.

For me it looks like the easiest solution. At least it works fine for ASP.NET Core 2.1 and Swashbuckle.AspNetCore 2.5.0.

这篇关于在Swagger上为.net Web API调用添加自定义标头字段的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆