๐Ÿ˜œ

์ญˆ๋‚˜์•„๋น  ๋ธ”๋กœ๊ทธ

JUNA
STUDIO

[ASP.Net] Adding JWT Authentication to Swagger in .NET

๋ฐœํ–‰์ผ: Feb, 2025
์กฐํšŒ์ˆ˜: 4
๋‹จ์–ด์ˆ˜: 34

Table of Contents

Overview

To enable JWT authentication in Swagger for your .NET application, add the following code to your project.


Implementation

Add the following code to configure Swagger with JWT authentication:

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

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

Tags: #Swagger#JWT Authentication#.NET#API Security#Bearer Token
JUNA BLOG VISITORS
Today
7
 (
updown
-7
)
Total
657
 (
updown
+7
)

ยฉ 2025 juniyunapapa@gmail.com.