• 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备注需要单独开启如下配置:

  • 相关阅读:
    IOS模拟器
    Android Monkey 测试实例
    Android log分析
    Android压力测试-Monkey
    IOS 压力测试-UI AutoMonkey
    Appium
    在chrome下鼠标拖动层变文本形状的问题
    学习笔记:利用GDI+生成简单的验证码图片
    利用FileStream实现多媒体文件复制
    C#读取Excel文件:通过OleDb连接,把excel文件作为数据源来读取
  • 原文地址:https://www.cnblogs.com/bu-dong/p/12271680.html
Copyright © 2020-2023  润新知