From 42ae72a1b7d3d1ec5f7813d98ffad2c029e6630a Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 20 Jun 2022 18:55:18 +0200 Subject: [PATCH] Using AppContext.BaseDirectory - as Assembly.Location might not be available in single-file applications. --- .../DbContextOptionsBuilderExtensions.cs | 2 +- .../Utilities/CryptographyHelperTests.cs | 2 +- AMWD.Common/Utilities/CryptographyHelper.cs | 15 ++++++--------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/AMWD.Common.EntityFrameworkCore/Extensions/DbContextOptionsBuilderExtensions.cs b/AMWD.Common.EntityFrameworkCore/Extensions/DbContextOptionsBuilderExtensions.cs index edf410f..c1849f1 100644 --- a/AMWD.Common.EntityFrameworkCore/Extensions/DbContextOptionsBuilderExtensions.cs +++ b/AMWD.Common.EntityFrameworkCore/Extensions/DbContextOptionsBuilderExtensions.cs @@ -208,7 +208,7 @@ namespace Microsoft.EntityFrameworkCore if (!Path.IsPathRooted(path)) { if (string.IsNullOrWhiteSpace(options.AbsoluteBasePath)) - options.AbsoluteBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + options.AbsoluteBasePath = AppContext.BaseDirectory; path = Path.Combine(options.AbsoluteBasePath, path); } diff --git a/AMWD.Common.Tests/Utilities/CryptographyHelperTests.cs b/AMWD.Common.Tests/Utilities/CryptographyHelperTests.cs index a702648..22ca96a 100644 --- a/AMWD.Common.Tests/Utilities/CryptographyHelperTests.cs +++ b/AMWD.Common.Tests/Utilities/CryptographyHelperTests.cs @@ -521,7 +521,7 @@ namespace AMWD.Common.Tests.Utilities public void ShouldCreateDefaultFile() { // arrange - string executingAssemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + string executingAssemblyDir = AppContext.BaseDirectory; string filePath = Path.Combine(executingAssemblyDir, "crypto.key"); // act diff --git a/AMWD.Common/Utilities/CryptographyHelper.cs b/AMWD.Common/Utilities/CryptographyHelper.cs index f5bab86..02115c4 100644 --- a/AMWD.Common/Utilities/CryptographyHelper.cs +++ b/AMWD.Common/Utilities/CryptographyHelper.cs @@ -1,5 +1,4 @@ using System.IO; -using System.Reflection; using System.Text; namespace System.Security.Cryptography @@ -19,16 +18,14 @@ namespace System.Security.Cryptography /// The (absolute) path to the crypto key file. On null the file 'crypto.key' at the executing assembly location will be used. public CryptographyHelper(string keyFile = null) { - if (string.IsNullOrWhiteSpace(keyFile)) - keyFile = "crypto.key"; - - if (!Path.IsPathRooted(keyFile)) - { - string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - keyFile = Path.Combine(dir, keyFile); - } masterKeyFile = keyFile; + if (string.IsNullOrWhiteSpace(masterKeyFile)) + masterKeyFile = "crypto.key"; + + if (!Path.IsPathRooted(masterKeyFile)) + masterKeyFile = Path.Combine(AppContext.BaseDirectory, masterKeyFile); + string pw = File.Exists(masterKeyFile) ? File.ReadAllText(masterKeyFile) : null; if (string.IsNullOrWhiteSpace(pw)) File.WriteAllText(masterKeyFile, GetRandomString(64));