Net Core教程

asp.net core swagger传bearer token

本文主要是介绍asp.net core swagger传bearer token,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在program.cs中注入swagger的Authorize支持

builder.Services.AddSwaggerGen(options =>
{
    OpenApiSecurityScheme scheme = new OpenApiSecurityScheme()
    {
        Description = "Authorization header, ex:'Bearer 123456abcdef'",
        Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Authorization" },
        Scheme = "oauth2",
        Name = "Authorization",
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.ApiKey,
    };

    options.AddSecurityDefinition("Authorization", scheme);
    OpenApiSecurityRequirement requirement = new OpenApiSecurityRequirement();
    requirement[scheme] = new List<string>();

    options.AddSecurityRequirement(requirement);
});

右上会出现Authorize按钮

由于没有输入jwt,调用hello出401错误

点击输入bearer %token%,之后再调用hello,可以看到header里包含了Authorization bearer信息,就没有问题了。

这篇关于asp.net core swagger传bearer token的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!