Created
April 16, 2021 18:50
-
-
Save nathy-mesquita/2078d426398cec3b8017518c516e8212 to your computer and use it in GitHub Desktop.
Add JWT Bearer Authorization to Swagger and ASP.NET Core
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 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) | |
{ | |
CultureInfo ci = new CultureInfo("pt-BR"); | |
CultureInfo.DefaultThreadCurrentCulture = ci; | |
CultureInfo.DefaultThreadCurrentUICulture = ci; | |
CultureInfo.CurrentCulture = ci; | |
CultureInfo.CurrentUICulture = ci; | |
Thread.CurrentThread.CurrentCulture = ci; | |
Thread.CurrentThread.CurrentUICulture = ci; | |
services.Configure<RequestLocalizationOptions>(options => | |
{ | |
options.DefaultRequestCulture = new RequestCulture("pt-BR"); | |
options.SupportedCultures = new List<CultureInfo> | |
{ | |
ci, | |
}; | |
options.SupportedUICultures = new List<CultureInfo> | |
{ | |
ci, | |
}; | |
}); | |
services.AddAuthentication(x => | |
{ | |
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}) | |
services.AddSwaggerGen(c => | |
{ | |
c.SwaggerDoc("v1", new OpenApiInfo | |
{ | |
Title = "API Exemplo", | |
Version = "v1" | |
}); | |
c.AddSecurityDefinition("bearer", new OpenApiSecurityScheme | |
{ | |
Description = "Token sem o prefixo bearer", | |
Type = SecuritySchemeType.Http, | |
BearerFormat = "JWT", | |
In = ParameterLocation.Header, | |
Scheme = "bearer" | |
}); | |
// Set the comments path for the Swagger JSON and UI. | |
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | |
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); | |
c.IncludeXmlComments(xmlPath); | |
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "API.Exemplo.xml")); | |
//c.CustomSchemaIds(d => d.FullName); | |
}).AddControllers(); | |
services.ConfigureMigrator(Configuration); | |
//AutoMapper | |
services.AddProfilesAutoMapperApplication(); | |
} | |
public void ConfigureContainer(ContainerBuilder builder) | |
=> builder.ConfigureApplicationDependencies(Configuration); | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app | |
, IWebHostEnvironment env | |
, IMigrationRunner migrationRunner) | |
{ | |
app.UseAuthentication(); | |
if (!env.IsProduction()) | |
{ | |
// Disponibilizar o swagger somente em ambiente de desenvolvimento/homologação | |
app.UseSwagger(); | |
app.UseSwaggerUI(c => | |
{ | |
c.RoutePrefix = string.Empty; | |
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API Exemplo - v1"); | |
}); | |
//Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment