diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index cbbe21b..435327e 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -55,7 +55,7 @@ namespace CarCareTracker.Controllers { try { - var configFileContents = System.IO.File.ReadAllText("config/userConfig.json"); + var configFileContents = System.IO.File.ReadAllText(StaticHelper.UserConfigPath); var existingUserConfig = System.Text.Json.JsonSerializer.Deserialize(configFileContents); if (existingUserConfig is not null) { @@ -69,7 +69,7 @@ namespace CarCareTracker.Controllers userConfig.UserNameHash = string.Empty; userConfig.UserPasswordHash = string.Empty; } - System.IO.File.WriteAllText("config/userConfig.json", System.Text.Json.JsonSerializer.Serialize(userConfig)); + System.IO.File.WriteAllText(StaticHelper.UserConfigPath, System.Text.Json.JsonSerializer.Serialize(userConfig)); return Json(true); } catch (Exception ex) { diff --git a/Controllers/LoginController.cs b/Controllers/LoginController.cs index e2432aa..e2c6d0b 100644 --- a/Controllers/LoginController.cs +++ b/Controllers/LoginController.cs @@ -1,4 +1,5 @@ -using CarCareTracker.Models; +using CarCareTracker.Helper; +using CarCareTracker.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Mvc; @@ -36,7 +37,7 @@ namespace CarCareTracker.Controllers //compare it against hashed credentials try { - var configFileContents = System.IO.File.ReadAllText("config/userConfig.json"); + var configFileContents = System.IO.File.ReadAllText(StaticHelper.UserConfigPath); var existingUserConfig = System.Text.Json.JsonSerializer.Deserialize(configFileContents); if (existingUserConfig is not null) { @@ -74,7 +75,7 @@ namespace CarCareTracker.Controllers { try { - var configFileContents = System.IO.File.ReadAllText("config/userConfig.json"); + var configFileContents = System.IO.File.ReadAllText(StaticHelper.UserConfigPath); var existingUserConfig = JsonSerializer.Deserialize(configFileContents); if (existingUserConfig is not null) { @@ -86,7 +87,7 @@ namespace CarCareTracker.Controllers existingUserConfig.UserNameHash = hashedUserName; existingUserConfig.UserPasswordHash = hashedPassword; } - System.IO.File.WriteAllText("config/userConfig.json", JsonSerializer.Serialize(existingUserConfig)); + System.IO.File.WriteAllText(StaticHelper.UserConfigPath, JsonSerializer.Serialize(existingUserConfig)); return Json(true); } catch (Exception ex) @@ -101,7 +102,7 @@ namespace CarCareTracker.Controllers { try { - var configFileContents = System.IO.File.ReadAllText("config/userConfig.json"); + var configFileContents = System.IO.File.ReadAllText(StaticHelper.UserConfigPath); var existingUserConfig = JsonSerializer.Deserialize(configFileContents); if (existingUserConfig is not null) { @@ -110,7 +111,7 @@ namespace CarCareTracker.Controllers existingUserConfig.UserNameHash = string.Empty; existingUserConfig.UserPasswordHash = string.Empty; } - System.IO.File.WriteAllText("config/userConfig.json", JsonSerializer.Serialize(existingUserConfig)); + System.IO.File.WriteAllText(StaticHelper.UserConfigPath, JsonSerializer.Serialize(existingUserConfig)); //destroy any login cookies. Response.Cookies.Delete("ACCESS_TOKEN"); return Json(true); diff --git a/Helper/StaticHelper.cs b/Helper/StaticHelper.cs index d52b33f..a7c2ca0 100644 --- a/Helper/StaticHelper.cs +++ b/Helper/StaticHelper.cs @@ -6,5 +6,6 @@ public static class StaticHelper { public static string DbName = "data/cartracker.db"; + public static string UserConfigPath = "config/userConfig.json"; } } diff --git a/Program.cs b/Program.cs index b913a37..307c0e8 100644 --- a/Program.cs +++ b/Program.cs @@ -24,7 +24,7 @@ if (!Directory.Exists("data")) } //Additional JsonFile -builder.Configuration.AddJsonFile("config/userConfig.json", optional: true, reloadOnChange: true); +builder.Configuration.AddJsonFile(StaticHelper.UserConfigPath, optional: true, reloadOnChange: true); //Configure Auth builder.Services.AddDataProtection();