Beging to add the authorization with a JWT token

This commit is contained in:
Sébastien André
2020-03-19 21:33:09 +01:00
parent 170ccbd9c5
commit 7bb702e46c
9 changed files with 132 additions and 17 deletions

View File

@@ -1,12 +1,16 @@
using Microsoft.AspNetCore.Builder;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using AutoMapper;
using skydiveLogs_api.Ioc;
using AutoMapper;
namespace skydiveLogs_api
@@ -23,6 +27,41 @@ namespace skydiveLogs_api
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// JWT
//services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
// .AddJwtBearer(options =>
// {
// options.SaveToken = true;
// options.TokenValidationParameters = new TokenValidationParameters()
// {
// ValidateIssuer = true,
// ValidateAudience = true,
// ValidateLifetime = true,
// ValidateIssuerSigningKey = true,
// ValidIssuer = "toto", // Configuration["jwt:issuer"],
// ValidAudience = "toto", // Configuration["jwt:issuer"],
// IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("tata" /* this.Configuration["jwt:key"] */))
// };
// });
var key = Encoding.ASCII.GetBytes("tata");
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
// CORS
var corsConf = Configuration.GetSection("Cors")
.GetChildren()
@@ -30,7 +69,7 @@ namespace skydiveLogs_api
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
builder =>
{
builder.WithOrigins(corsConf["FrontUrl"])
.AllowAnyHeader()
@@ -61,6 +100,8 @@ namespace skydiveLogs_api
app.UseCors();
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}