为 Swagger 上的 .net web api 调用添加自定义 header 字段的最佳做法是什么?
What are the best practices in adding custom header fields for a .net web api call on Swagger?
我在网上看到了很多方法,但是大部分都是乱七八糟的,我就用这两种方法
使用范围,我为手机做了一个,另一个为网站做了
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 调用,如您所见,我有一个移动授权和一个 Web 授权,所以我的 api 调用看起来像这样:
[HttpGet]
[Route("something")]
[WebAuthorize(Code = PermissionCode, Type =PermissionType)]
public async Task<Dto> Getsomething()
{
return await unitOfWork.GetService<ISomething>().GetSomething();
}
我在使用范围时面临的问题是所有具有网络授权的调用都将共享相同的 headers,因此对于特殊调用,我使用另一种方式添加自定义 headers。
使用 apiDescription.RelativePath,如果相对路径等于 api 调用,我将检查它我想添加自定义 header,示例:
[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 中添加以下内容
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
});
}
起初这是方便且足够好的修复,但事情变得很难看,因为我添加了很多需要自定义 headers 的调用,如果相同的 URL 有一个 Get 和Post那就更丑了
我正在寻找处理这个问题的最佳方法。
可以将属性 [FromHeader] 用于应在自定义 headers 中发送的 Web 方法参数(或模型 class 中的属性)。是这样的:
[HttpGet]
public ActionResult Products([FromHeader(Name = "User-Identity")]string userIdentity)
对我来说,这似乎是最简单的解决方案。至少它适用于 ASP.NET Core 2.1 和 Swashbuckle.AspNetCore 2.5.0.
我在网上看到了很多方法,但是大部分都是乱七八糟的,我就用这两种方法
使用范围,我为手机做了一个,另一个为网站做了
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 调用,如您所见,我有一个移动授权和一个 Web 授权,所以我的 api 调用看起来像这样:
[HttpGet] [Route("something")] [WebAuthorize(Code = PermissionCode, Type =PermissionType)] public async Task<Dto> Getsomething() { return await unitOfWork.GetService<ISomething>().GetSomething(); }
我在使用范围时面临的问题是所有具有网络授权的调用都将共享相同的 headers,因此对于特殊调用,我使用另一种方式添加自定义 headers。
使用 apiDescription.RelativePath,如果相对路径等于 api 调用,我将检查它我想添加自定义 header,示例:
[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 中添加以下内容
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 }); }
起初这是方便且足够好的修复,但事情变得很难看,因为我添加了很多需要自定义 headers 的调用,如果相同的 URL 有一个 Get 和Post那就更丑了
我正在寻找处理这个问题的最佳方法。
可以将属性 [FromHeader] 用于应在自定义 headers 中发送的 Web 方法参数(或模型 class 中的属性)。是这样的:
[HttpGet]
public ActionResult Products([FromHeader(Name = "User-Identity")]string userIdentity)
对我来说,这似乎是最简单的解决方案。至少它适用于 ASP.NET Core 2.1 和 Swashbuckle.AspNetCore 2.5.0.