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>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>skydiveLogs_api.Business</RootNamespace>
</PropertyGroup>

View File

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

View File

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

View File

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

View File

@@ -64,31 +64,29 @@ namespace skydiveLogs_api.Controllers
private string CreateToken(UserReq model)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes("tata");
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, model.Login)
}),
Expires = DateTime.UtcNow.AddMinutes(30),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
//var tokenHandler = new JwtSecurityTokenHandler();
//var key = Encoding.ASCII.GetBytes("azertyuiopqsdfghjklmwxcvbn");
//var tokenDescriptor = new SecurityTokenDescriptor
//{
// Subject = new ClaimsIdentity(new Claim[]
// {
// new Claim(ClaimTypes.Name, model.Login)
// }),
// Expires = DateTime.UtcNow.AddMinutes(30),
// SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
//};
//var token = tokenHandler.CreateToken(tokenDescriptor);
//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"] */));
//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);
return new JwtSecurityTokenHandler().WriteToken(token);
}
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.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
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("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
};
});
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")
@@ -85,9 +86,9 @@ namespace skydiveLogs_api
}
// 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();
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<RootNamespace>skydiveLogs_api</RootNamespace>
<UserSecretsId>9d7d268e-ee4d-43a2-a9b7-5b00b516f6f8</UserSecretsId>
@@ -21,8 +21,10 @@
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<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.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>