From 9b287f08b8e2c8d091967597bc9610afea8058ca Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 12 Dec 2020 14:47:33 +0200 Subject: Improved appsettings.json reading --- API/Controllers/UserController.cs | 6 +++--- API/Service/UserService.cs | 10 +++++----- API/Startup.cs | 5 ++++- 3 files changed, 12 insertions(+), 9 deletions(-) (limited to 'API') diff --git a/API/Controllers/UserController.cs b/API/Controllers/UserController.cs index fd94283..ceeee33 100644 --- a/API/Controllers/UserController.cs +++ b/API/Controllers/UserController.cs @@ -5,7 +5,7 @@ using AutoMapper; using Microsoft.AspNetCore.Mvc; using Data.Models.DTOs; using Microsoft.AspNetCore.Authorization; -using Data.Models.Classes; +using Data.Models.Options; using Microsoft.Extensions.Configuration; namespace API.Controllers @@ -16,9 +16,9 @@ namespace API.Controllers { private readonly UserService _service; - public UserController(DevHiveContext context, IMapper mapper, IConfiguration configuration) + public UserController(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions) { - this._service = new UserService(context, mapper, configuration.GetSection("AppSettings")); + this._service = new UserService(context, mapper, jwtOptions); } [HttpPost] diff --git a/API/Service/UserService.cs b/API/Service/UserService.cs index c4f6e92..797a924 100644 --- a/API/Service/UserService.cs +++ b/API/Service/UserService.cs @@ -4,7 +4,7 @@ using AutoMapper; using Data.Models.Classes; using Data.Models.DTOs; using Microsoft.AspNetCore.Mvc; - +using Data.Models.Options; using System.IdentityModel.Tokens.Jwt; using Microsoft.IdentityModel.Tokens; using System.Security.Claims; @@ -18,13 +18,13 @@ namespace API.Service { private readonly UserDbRepository _userDbRepository; private readonly IMapper _userMapper; - private readonly IConfiguration _appSettings; + private readonly JWTOptions _jwtOptions; - public UserService(DevHiveContext context, IMapper mapper, IConfiguration appSettings) + public UserService(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions) { this._userDbRepository = new UserDbRepository(context); this._userMapper = mapper; - this._appSettings = appSettings; + this._jwtOptions = jwtOptions; } public async Task LoginUser(LoginDTO loginDTO) @@ -35,7 +35,7 @@ namespace API.Service return new NotFoundObjectResult("User does not exist!"); // Get key from appsettings.json - var key = Encoding.ASCII.GetBytes(_appSettings.GetSection("Secret").Value); + var key = Encoding.ASCII.GetBytes(_jwtOptions.Secret); if (user.PasswordHash != GeneratePasswordHash(loginDTO.Password)) return new BadRequestObjectResult("Incorrect password!"); diff --git a/API/Startup.cs b/API/Startup.cs index 46d9fc6..b58311d 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -10,7 +10,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Data.Models.Classes; - +using Data.Models.Options; using Microsoft.IdentityModel.Tokens; using Microsoft.AspNetCore.Authentication.JwtBearer; using System.Text; @@ -45,6 +45,9 @@ namespace API options.Password.RequiredLength = 5; }); + services.AddSingleton( + new JWTOptions(Configuration.GetSection("AppSettings").GetSection("Secret").Value)); + // Get key from appsettings.json var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings").GetSection("Secret").Value); // Setup Jwt Authentication -- cgit v1.2.3