.net core swaggerUI 添加自定義的token 驗證


最開始用的JWT, 使用以下配置;

        //AddSwaggerGen
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme.",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Scheme = "bearer",
                    Type = SecuritySchemeType.Http,
                    BearerFormat = "JWT"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
                        },
                        new List<string>()
                    }
                });

 

后續項目使用自定義的token 驗證規則,使用以下配置

         //AddSwaggerGen
                c.AddSecurityDefinition("token", new OpenApiSecurityScheme
                {
                    Description = "header token",
                    Name = "token",
                    In = ParameterLocation.Header,
                    Scheme = "",
                    Type = SecuritySchemeType.ApiKey,//設置類型
                    BearerFormat = ""
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "token" }
                        },
                        new List<string>()
                    }
                });

效果如下,輸入你的token:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM