Update to DotNet Core 3.1

+ next step to add JWT token authorize
This commit is contained in:
Sébastien André
2020-03-19 22:17:46 +01:00
parent 7bb702e46c
commit 4a67b9a5f6
7 changed files with 69 additions and 68 deletions

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>skydiveLogs_api.Business</RootNamespace> <RootNamespace>skydiveLogs_api.Business</RootNamespace>
</PropertyGroup> </PropertyGroup>

View File

@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>skydiveLogs_api.Data</RootNamespace> <RootNamespace>skydiveLogs_api.Data</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="LiteDB" Version="5.0.3" /> <PackageReference Include="LiteDB" Version="5.0.4" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>skydiveLogs_api.Ioc</RootNamespace> <RootNamespace>skydiveLogs_api.Ioc</RootNamespace>
</PropertyGroup> </PropertyGroup>

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>skydiveLogs_api.Model</RootNamespace> <RootNamespace>skydiveLogs_api.Model</RootNamespace>
</PropertyGroup> </PropertyGroup>

View File

@@ -64,31 +64,29 @@ namespace skydiveLogs_api.Controllers
private string CreateToken(UserReq model) private string CreateToken(UserReq model)
{ {
var tokenHandler = new JwtSecurityTokenHandler(); //var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes("tata"); //var key = Encoding.ASCII.GetBytes("azertyuiopqsdfghjklmwxcvbn");
var tokenDescriptor = new SecurityTokenDescriptor //var tokenDescriptor = new SecurityTokenDescriptor
{ //{
Subject = new ClaimsIdentity(new Claim[] // Subject = new ClaimsIdentity(new Claim[]
{ // {
new Claim(ClaimTypes.Name, model.Login) // new Claim(ClaimTypes.Name, model.Login)
}), // }),
Expires = DateTime.UtcNow.AddMinutes(30), // Expires = DateTime.UtcNow.AddMinutes(30),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) // SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
}; //};
var token = tokenHandler.CreateToken(tokenDescriptor); //var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token); //return tokenHandler.WriteToken(token);
var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes("azertyuiopqsdfghjklmwxcvbn" /* this._configuration["jwt:key"] */));
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken("toto" /* this._configuration["jwt:issuer"] */,
"toto" /* this._configuration["jwt:issuer"] */,
expires: System.DateTime.Now.AddMinutes(30),
signingCredentials: credentials);
//var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes("tata" /* this._configuration["jwt:key"] */)); return new JwtSecurityTokenHandler().WriteToken(token);
//var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
//var token = new JwtSecurityToken("toto" /* this._configuration["jwt:issuer"] */,
// "toto" /* this._configuration["jwt:issuer"] */,
// expires: System.DateTime.Now.AddMinutes(30),
// signingCredentials: credentials);
//return new JwtSecurityTokenHandler().WriteToken(token);
} }
private readonly IUserService _userService; private readonly IUserService _userService;

View File

@@ -25,42 +25,43 @@ namespace skydiveLogs_api
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddMvc(options => { options.EnableEndpointRouting = false; })
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
// JWT // JWT
//services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
// .AddJwtBearer(options => .AddJwtBearer(options =>
// { {
// options.SaveToken = true; options.SaveToken = true;
// options.TokenValidationParameters = new TokenValidationParameters() options.TokenValidationParameters = new TokenValidationParameters()
// { {
// ValidateIssuer = true, ValidateIssuer = true,
// ValidateAudience = true, ValidateAudience = true,
// ValidateLifetime = true, ValidateLifetime = true,
// ValidateIssuerSigningKey = true, ValidateIssuerSigningKey = true,
// ValidIssuer = "toto", // Configuration["jwt:issuer"], ValidIssuer = "toto", // Configuration["jwt:issuer"],
// ValidAudience = "toto", // Configuration["jwt:issuer"], ValidAudience = "toto", // Configuration["jwt:issuer"],
// IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("tata" /* this.Configuration["jwt:key"] */)) IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("azertyuiopqsdfghjklmwxcvbn" /* this.Configuration["jwt:key"] */))
// }; };
// }); });
var key = Encoding.ASCII.GetBytes("tata"); //var key = Encoding.ASCII.GetBytes("azertyuiopqsdfghjklmwxcvbn");
services.AddAuthentication(x => //services.AddAuthentication(x =>
{ //{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; // x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}) //})
.AddJwtBearer(x => //.AddJwtBearer(x =>
{ //{
x.RequireHttpsMetadata = false; // x.RequireHttpsMetadata = false;
x.SaveToken = true; // x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters // x.TokenValidationParameters = new TokenValidationParameters
{ // {
ValidateIssuerSigningKey = true, // ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key), // IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false, // ValidateIssuer = false,
ValidateAudience = false // ValidateAudience = false
}; // };
}); //});
// CORS // CORS
var corsConf = Configuration.GetSection("Cors") var corsConf = Configuration.GetSection("Cors")
@@ -85,9 +86,9 @@ namespace skydiveLogs_api
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
if (env.IsDevelopment()) if (env.EnvironmentName == "Development")
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<RootNamespace>skydiveLogs_api</RootNamespace> <RootNamespace>skydiveLogs_api</RootNamespace>
<UserSecretsId>9d7d268e-ee4d-43a2-a9b7-5b00b516f6f8</UserSecretsId> <UserSecretsId>9d7d268e-ee4d-43a2-a9b7-5b00b516f6f8</UserSecretsId>
@@ -21,8 +21,10 @@
<PackageReference Include="AutoMapper" Version="9.0.0" /> <PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>