Last active
February 25, 2022 17:22
-
-
Save daniaalnadir/ce7fbd764b345a687d2444d757bda827 to your computer and use it in GitHub Desktop.
Swagger Authentication Setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApiKeyAuthenticationDefaults | |
{ | |
public const string AuthenticationScheme = "ApiKey"; | |
public const string HeaderName = "Authorization"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JwtAuthenticationDefaults | |
{ | |
public const string AuthenticationScheme = "JWT"; | |
public const string HeaderName = "Authorization"; | |
public const string BearerPrefix = "Bearer"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSwaggerGen(c => | |
{ | |
c.SwaggerDoc("v1", new OpenApiInfo {Title = "TestWebApi", Version = "v1"}); | |
c.AddSecurityDefinition(JwtAuthenticationConst.AuthenticationScheme, | |
new OpenApiSecurityScheme | |
{ | |
Description = "JWT Authorization header using the Bearer scheme.", | |
Name = JwtAuthenticationConst.HeaderName, | |
In = ParameterLocation.Header, | |
Type = SecuritySchemeType.Http, | |
Scheme = "bearer" | |
}); | |
c.AddSecurityDefinition(ApiKeyAuthenticationConst.AuthenticationScheme, | |
new OpenApiSecurityScheme | |
{ | |
Description = "API key used in the Authorization header.", | |
Name = ApiKeyAuthenticationConst.HeaderName, | |
In = ParameterLocation.Header, | |
Type = SecuritySchemeType.ApiKey | |
}); | |
c.AddSecurityRequirement(new OpenApiSecurityRequirement | |
{ | |
{ | |
new OpenApiSecurityScheme | |
{ | |
Reference = new OpenApiReference | |
{ | |
Type = ReferenceType.SecurityScheme, | |
Id = JwtAuthenticationConst.AuthenticationScheme | |
} | |
}, | |
new List<string>() | |
} | |
}); | |
c.AddSecurityRequirement(new OpenApiSecurityRequirement | |
{ | |
{ | |
new OpenApiSecurityScheme | |
{ | |
Reference = new OpenApiReference | |
{ | |
Type = ReferenceType.SecurityScheme, | |
Id = ApiKeyAuthenticationConst.AuthenticationScheme | |
} | |
}, | |
new List<string>() | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment