Convert the AppSettings to model class
This commit is contained in:
9
Back/skydiveLogs-api.Model/CorsSettings.cs
Normal file
9
Back/skydiveLogs-api.Model/CorsSettings.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace skydiveLogs_api.Model
|
||||
{
|
||||
public class CorsSettings
|
||||
{
|
||||
public string FrontUrl { get; set; }
|
||||
}
|
||||
}
|
||||
11
Back/skydiveLogs-api.Model/JwtSettings.cs
Normal file
11
Back/skydiveLogs-api.Model/JwtSettings.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace skydiveLogs_api.Model
|
||||
{
|
||||
public class JwtSettings
|
||||
{
|
||||
public string Issuer { get; set; }
|
||||
|
||||
public string Passphrase { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,10 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Security.Claims;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using AutoMapper;
|
||||
|
||||
@@ -25,13 +24,11 @@ namespace skydiveLogs_api.Controllers
|
||||
{
|
||||
public UserController(IUserService userService,
|
||||
IMapper mapper,
|
||||
IConfiguration configuration)
|
||||
IOptions<JwtSettings> jwtSettings)
|
||||
{
|
||||
_userService = userService;
|
||||
_mapper = mapper;
|
||||
_jwtConf = configuration.GetSection("JWT")
|
||||
.GetChildren()
|
||||
.ToDictionary(d => d.Key, d => d.Value);
|
||||
_jwtConf = jwtSettings.Value;
|
||||
}
|
||||
|
||||
// GET: api/User/AlwayLogin
|
||||
@@ -76,9 +73,8 @@ namespace skydiveLogs_api.Controllers
|
||||
{
|
||||
IActionResult result;
|
||||
var newUser = _mapper.Map<User>(userToAdd);
|
||||
var userAdded = _userService.AddNewUser(newUser);
|
||||
|
||||
if (!userAdded)
|
||||
if (!_userService.AddNewUser(newUser))
|
||||
{
|
||||
result = BadRequest(new { message = "Error during the creation of the user." });
|
||||
}
|
||||
@@ -96,17 +92,18 @@ namespace skydiveLogs_api.Controllers
|
||||
|
||||
private string CreateToken(UserResp foundUser)
|
||||
{
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtConf["Key"]));
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtConf.Passphrase));
|
||||
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
var token = new JwtSecurityToken(issuer: _jwtConf["Issuer"],
|
||||
audience: _jwtConf["Issuer"],
|
||||
var token = new JwtSecurityToken(issuer: _jwtConf.Issuer,
|
||||
audience: _jwtConf.Issuer,
|
||||
expires: DateTime.Now.AddDays(1),
|
||||
signingCredentials: credentials,
|
||||
claims: new Claim[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, foundUser.Login),
|
||||
new Claim(ClaimTypes.UserData, foundUser.Id.ToString())
|
||||
new Claim(ClaimTypes.UserData, foundUser.Id.ToString()),
|
||||
new Claim(ClaimTypes.Email, foundUser.Email)
|
||||
});
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
@@ -114,6 +111,6 @@ namespace skydiveLogs_api.Controllers
|
||||
|
||||
private readonly IUserService _userService;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly Dictionary<string, string> _jwtConf;
|
||||
private readonly JwtSettings _jwtConf;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@
|
||||
"latitude": "44.00109",
|
||||
"longitude": "4.75815",
|
||||
"name": "Skydive Pujaut",
|
||||
"address": null,
|
||||
"address": "Aérodrome Avignon-Pujaut</br>30131 Pujaut",
|
||||
"website": "www.skydive-pujaut.com",
|
||||
"email": null,
|
||||
"email": "info@skydivepujaut.com",
|
||||
"type": [
|
||||
"dz"
|
||||
],
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Email { get; set; }
|
||||
|
||||
public string FirstName { get; set; }
|
||||
|
||||
public string LastName { get; set; }
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System;
|
||||
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -15,6 +13,8 @@ using AutoMapper;
|
||||
|
||||
using skydiveLogs_api.Ioc;
|
||||
using skydiveLogs_api.Business.Interface;
|
||||
using skydiveLogs_api.Model;
|
||||
|
||||
|
||||
namespace skydiveLogs_api
|
||||
{
|
||||
@@ -32,9 +32,12 @@ namespace skydiveLogs_api
|
||||
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
|
||||
|
||||
// JWT
|
||||
var jwtConf = Configuration.GetSection("JWT")
|
||||
.GetChildren()
|
||||
.ToDictionary(d => d.Key, d => d.Value);
|
||||
var jwtSection = Configuration.GetSection("JWT");
|
||||
services.Configure<JwtSettings>(jwtSection);
|
||||
|
||||
var jwtSettings = new JwtSettings();
|
||||
jwtSection.Bind(jwtSettings);
|
||||
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
@@ -45,22 +48,22 @@ namespace skydiveLogs_api
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = jwtConf["Issuer"],
|
||||
ValidAudience = jwtConf["Issuer"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConf["Key"]))
|
||||
ValidIssuer = jwtSettings.Issuer,
|
||||
ValidAudience = jwtSettings.Issuer,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Passphrase))
|
||||
};
|
||||
});
|
||||
|
||||
// CORS
|
||||
var corsConf = Configuration.GetSection("Cors")
|
||||
.GetChildren()
|
||||
.ToDictionary(d => d.Key, d => d.Value);
|
||||
var corsSettings = new CorsSettings();
|
||||
Configuration.GetSection("Cors").Bind(corsSettings);
|
||||
|
||||
services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(
|
||||
builder =>
|
||||
{
|
||||
builder.WithOrigins(corsConf["FrontUrl"])
|
||||
builder.WithOrigins(corsSettings.FrontUrl)
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
@@ -106,7 +109,6 @@ namespace skydiveLogs_api
|
||||
var initDbService = serviceProvider.GetRequiredService<IInitDbService>();
|
||||
initDbService.GenerateDb();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
},
|
||||
"JWT": {
|
||||
"Issuer": "NoIdea",
|
||||
"Key": "the very long and strong passphrase to encrypt the token"
|
||||
"Passphrase": "the very long and strong passphrase to encrypt the token"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
|
||||
Reference in New Issue
Block a user