I'm developing a platform with ASP.NET Core 5 that uses Identity as a membership system. Aside from the web application, this platform also exposes a Web API. I have tried using JWT as Web API authentication. But when I add the JWT configuration I'm no longer able to login to the web application.
This is the
Startup.cs
file:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
private MappingProfile mappingProfile;
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<ApplicationRole>() //Line that can help you
.AddEntityFrameworkStores<ApplicationDbContext>();
// JWT
// If it is commented, I can login to the web app, if not I can't
/*services
//.AddHttpContextAccessor()
//.AddAuthorization()
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});*/
// AUTOMAPPER
this.mappingProfile = new MappingProfile();
MapperConfiguration mappingConfig = new MapperConfiguration(mc => {
mc.AddProfile(this.mappingProfile);
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddControllers();
services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddSignalR();
// REPOSITORIES
services.AddScoped<IExampleRepository, ExampleRepository>();
// LOCALIZATION
services.AddLocalization(/*options => options.ResourcesPath = "Resources"*/);
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("es")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationDbContext context, RoleManager<ApplicationRole> roleManager, IStringLocalizer<MappingProfile> mappingProfileLocalizer)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapHub<DcHub>("/dchub");
});
this.mappingProfile.Localizer = mappingProfileLocalizer;
ApplicationDbInitializer.Initialize(context, roleManager);
}
}
I think that I'm missing something in this configuration.
In case it can be of help, information about the development environment and libraries:
- Microsoft Visual Studio Professional 2019 (Version 16.11.15)
- .NET 5