C# ASP Net Core 2.2仅将锁定器图标添加到需要授权的方法-Swagger UI 版本: ASP Net核心Web API-2.2 Swashback.AspNetCore-4.0.1 我现在有什么?
我已经在我的WebAPI项目中实现了swagger。我在需要它的方法上使用带有C# ASP Net Core 2.2仅将锁定器图标添加到需要授权的方法-Swagger UI 版本: ASP Net核心Web API-2.2 Swashback.AspNetCore-4.0.1 我现在有什么?,c#,asp.net-core,authorization,swagger,swashbuckle,C#,Asp.net Core,Authorization,Swagger,Swashbuckle,我已经在我的WebAPI项目中实现了swagger。我在需要它的方法上使用带有[Authorize]属性的JWT授权 所以我想要一种简单的方法来发送需要授权的请求。在我的ConfigureServices类中,我添加了以下逻辑 services.AddSwaggerGen(c=> { //其他招摇过市的选择 c、 AddSecurityDefinition(“持有人”),新ApiKeyScheme { In=“header”, Description=“请按空格和您的JWT令牌在字段中输入单词‘
[Authorize]
属性的JWT授权
所以我想要一种简单的方法来发送需要授权的请求。在我的ConfigureServices
类中,我添加了以下逻辑
services.AddSwaggerGen(c=>
{
//其他招摇过市的选择
c、 AddSecurityDefinition(“持有人”),新ApiKeyScheme
{
In=“header”,
Description=“请按空格和您的JWT令牌在字段中输入单词‘Bearer’”,
Name=“授权”,
Type=“apiKey”
});
c、 AddSecurityRequest(新字典)
{
{“Bearer”,Enumerable.Empty()},
});
//其他招摇过市的选择
});
其作用如下:它在swagger-Authority中添加了一个新按钮 问题是,它还在每个方法旁边添加了一个“打开”储物柜图标。尽管如此,其中一些需要授权 当我使用authorize按钮成功地进行授权时(它基本上会向每个请求添加头授权),我会在所有请求上收到一个“关闭”的锁。 我知道这可能是期望的功能,用于指示将通过请求发送授权令牌。我想要一种方法来显示哪些方法需要授权,哪些不需要授权 我想要什么? 例如,匿名方法的“打开”锁和具有
[Authorize]
属性的方法的“关闭”锁
它可以是一个额外的图标,旁边的这个或修改这个行为,没有问题。我怎样才能做到这一点
可能的解决方案?
我认为一个可能的解决方案是制作一个OperationFilter并遍历所有方法,只将“某物”附加到那些具有
[Authorize]
属性的方法上。这是最好的解决方案吗?如果是这样,您将如何实施它?因为我问这个问题已经一个多月了。我是这样做的
我从Startup.cs
中删除了以下代码:
c.AddSecurityDefinition(“持有人”),新的ApiKeyScheme
{
In=“header”,
Description=“请按空格和您的JWT令牌在字段中输入单词‘Bearer’”,
Name=“授权”,
Type=“apiKey”
});
c、 AddSecurityRequest(新字典)
{
{“Bearer”,Enumerable.Empty()},
});
我又加了一句:
c.操作过滤器
注意:因此,如果您想测试API,首先需要获得一个令牌,然后在需要的地方填充它。对于Swashbuckle 5.0.0和更高版本,将@G.Dimov的答案中的AddAuthHeaderOperationFilter更改为以下内容(进行一些额外的样式编辑):
然后将引用定义的安全要求添加到操作中:
public class AddAuthorizationHeaderOperationHeader : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var actionMetadata = context.ApiDescription.ActionDescriptor.EndpointMetadata;
var isAuthorized = actionMetadata.Any(metadataItem => metadataItem is AuthorizeAttribute);
var allowAnonymous = actionMetadata.Any(metadataItem => metadataItem is AllowAnonymousAttribute);
if (!isAuthorized || allowAnonymous)
{
return;
}
if (operation.Parameters == null)
operation.Parameters = new List<OpenApiParameter>();
operation.Security = new List<OpenApiSecurityRequirement>();
//Add JWT bearer type
operation.Security.Add(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
// Definition name.
// Should exactly match the one given in the service configuration
Id = "Bearer"
}
}, new string[0]
}
}
);
}
}
公共类AddAuthorizationHeaderOperationHeader:IOperationFilter
{
公共无效应用(OpenApiOperation操作,OperationFilterContext上下文)
{
var actionMetadata=context.apiscription.ActionDescriptor.EndpointMetadata;
var isAuthorized=actionMetadata.Any(metadataItem=>metadataItem是Authorized属性);
var allowAnonymous=actionMetadata.Any(metadataItem=>metadataItem是AllowAnonymousAttribute);
如果(!isAuthorized | | allowAnonymous)
{
返回;
}
if(operation.Parameters==null)
operation.Parameters=newlist();
operation.Security=newlist();
//添加JWT承载类型
operation.Security.Add(新的OpenApiSecurityRequirement
{
{
新的OpenApiSecurityScheme
{
Reference=新的OpenApiReference
{
Type=ReferenceType.SecurityScheme,
//定义名称。
//应该与服务配置中给出的完全匹配
Id=“持票人”
}
},新字符串[0]
}
}
);
}
}
您好,您可以使用AddAuthHeaderOperationFilter.cs所需的提供吗?使用Microsoft.AspNetCore.Http;使用Microsoft.AspNetCore.Mvc.Authorization;使用swashback.AspNetCore.Swagger;使用swashback.AspNetCore.SwaggerGen;使用制度;使用System.Collections.Generic;使用System.Linq;使用System.Threading.Tasks代码>很好的一个,很高兴它有帮助。最好的解决方案版本5我会说!
public void ConfigureServices(IServiceCollection services)
{
// Service configuration
services.AddSwaggerGen(c =>
{
// Configure Swagger
// "Bearer" is the name for this definition. Any other name could be used
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "Use bearer token to authorize",
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT"
});
}
}
public class AddAuthorizationHeaderOperationHeader : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var actionMetadata = context.ApiDescription.ActionDescriptor.EndpointMetadata;
var isAuthorized = actionMetadata.Any(metadataItem => metadataItem is AuthorizeAttribute);
var allowAnonymous = actionMetadata.Any(metadataItem => metadataItem is AllowAnonymousAttribute);
if (!isAuthorized || allowAnonymous)
{
return;
}
if (operation.Parameters == null)
operation.Parameters = new List<OpenApiParameter>();
operation.Security = new List<OpenApiSecurityRequirement>();
//Add JWT bearer type
operation.Security.Add(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
// Definition name.
// Should exactly match the one given in the service configuration
Id = "Bearer"
}
}, new string[0]
}
}
);
}
}