小编Ray*_*Ray的帖子

根据角色/api_key 生成 Swashbuckle API 文档

我正在使用 Swashbuckle (5.3.2),它生成了一个很好的 API 文档。

为了澄清我的问题,我建立了一个没有实际意义的小示例项目。

API 只能与有效的 API 密钥一起使用。为此,我引入了一个 ApiKeyFilter 来验证 api_key 并读出相应的角色。

ApiKey过滤器

public class ApiKeyFilter : IAuthenticationFilter
{
    private static Dictionary<string, String[]> allowedApps = new Dictionary<string, String[]>();
    private readonly string authenticationScheme = "Bearer";
    private readonly string queryStringApiKey = "api_key";

    public bool AllowMultiple
    {
        get { return false; }
    }

    public ApiKeyFilter()
    {
        if (allowedApps.Count == 0)
        {
            allowedApps.Add("PetLover_api_key", new []{"PetLover"});
            allowedApps.Add("CarOwner_api_key", new []{"CarOwner"});
            allowedApps.Add("Admin_api_key", new []{"PetLover","CarOwner"});
        }
    }

    public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
        var req …
Run Code Online (Sandbox Code Playgroud)

c# .net-4.5 asp.net-web-api swagger swashbuckle

4
推荐指数
1
解决办法
1903
查看次数

标签 统计

.net-4.5 ×1

asp.net-web-api ×1

c# ×1

swagger ×1

swashbuckle ×1