• AspNetCore 3.1下使用Swagger 5.0授权问题


    依赖包:
    netcoreapp3.1
    IdentityModel 4.1.1
    Swashbuckle.AspNetCore 5.0.0
    Microsoft.AspNetCore.Authentication.JwtBearer 3.1.1

    登录是依赖于IdentityServer4。
    客户端是API项目,使用了.NET Core 3.1,安装了Swagger做接口ui。
    如下为API项目的Startup.cs文件内容:

        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddRepoExtentions(Configuration);
                services.AddControllers();
                services.AddAuthentication("Bearer")
                    .AddJwtBearer("Bearer", options =>
                    {
                        options.Authority = "https://localhost:5001";
                        options.RequireHttpsMetadata = false;
    
                        options.Audience = "api1";
                    });
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); 
                    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                    c.IncludeXmlComments(xmlPath,true); //添加控制器层注释(true表示显示控制器注释)
                    
                    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                    {
                        Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)"",
                        Name = "Authorization",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey,
                        BearerFormat = "JWT",
                        Scheme = "Bearer"
                    });
    
                    c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                    {
                        {
                            new OpenApiSecurityScheme
                            {
                                Reference = new OpenApiReference {
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Bearer"
                                }
                            },
                            new string[] { }
                        }
                    });
                });
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthentication();
                app.UseAuthorization();
                
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                });
                app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
            }
        }
    

    其中xml备注需要单独开启如下配置:

  • 相关阅读:
    rac启动维护笔记
    cache-fusion笔记
    RAC配置笔记
    记一次异机rman还原后的操作
    索引小结
    DBlink的创建与删除
    小说经典语录
    SQL通配符
    ArrayList集合详解
    Oracle数据库四种数据完整性约束
  • 原文地址:https://www.cnblogs.com/bu-dong/p/12271680.html
Copyright © 2020-2023  润新知