Files
SkydiveLogs/Back/skydiveLogs-api/Startup.cs
Sébastien André 4a67b9a5f6 Update to DotNet Core 3.1
+ next step to add JWT token authorize
2020-03-19 22:17:46 +01:00

112 lines
4.1 KiB
C#

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 Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using AutoMapper;
using skydiveLogs_api.Ioc;
namespace skydiveLogs_api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => { options.EnableEndpointRouting = false; })
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
// 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("azertyuiopqsdfghjklmwxcvbn" /* this.Configuration["jwt:key"] */))
};
});
//var key = Encoding.ASCII.GetBytes("azertyuiopqsdfghjklmwxcvbn");
//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()
.ToDictionary(d => d.Key, d => d.Value);
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins(corsConf["FrontUrl"])
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// IoC
var iocService = new IocService(services, Configuration);
iocService.Configure();
services.AddAutoMapper(typeof(Mapper.ModelProfile));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.EnvironmentName == "Development")
{
app.UseDeveloperExceptionPage();
}
else
{
// 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.UseCors();
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
public IConfiguration Configuration { get; }
}
}