.Net Core 警告未配置 XML 加密器
.Net Core warning No XML encryptor configured
当我开始我的服务时(API on .Net Core 2.2 in Docker container)我收到警告:
No XML encryptor configured. Key
{daa53741-8295-4c9b-ae9c-e69b003f16fa} may be persisted to storage in
unencrypted form.
我没有配置 DataProtection。我找到了配置 DataProtection 的解决方案,但我不需要保存此密钥。对我来说,如果密钥只会保留到应用程序重新启动 - 没关系。但是我不需要在日志中看到这个警告
有什么想法吗?我们该怎么做?
我的创业公司 Class 看起来像这样:
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services) {
services.AddMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddApiVersioning(o => o.ApiVersionReader = new HeaderApiVersionReader("api-version"));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseMvc();
lifetime.ApplicationStarted.Register(OnApplicationStarted);
lifetime.ApplicationStopping.Register(OnShutdown);
}
public void OnApplicationStarted() {
Console.Out.WriteLine($"Open Api Started");
}
public void OnShutdown() {
Console.Out.WriteLine($"Open Api is shutting down.");
}
}
也许它也有帮助我在项目中的包
<ItemGroup>
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.5" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.5.4" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.18.6" />
</ItemGroup>
这可能是权限错误,但您必须确定。抛出此错误时尝试登录。我看到在开发环境中出现此错误的原因有很多,通常是文件读取权限或找不到文件或没有文件。
用下面的日志记录算法包装你的主要功能,看看有什么问题:
public static void Main(string[] args)
{
CurrentDirectoryHelpers.SetCurrentDirectory();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Serilog", LogEventLevel.Information)
.WriteTo.File("Logs/LogFrom_ProgramMain.txt")
.CreateLogger();
try
{
var whb = WebHost.CreateDefaultBuilder(args).UseContentRoot(Directory.GetCurrentDirectory());
//whb... your codes
Log.Logger.Information("Information:blabla");
}
catch(Exception ex)
{
Log.Logger.Error("Main handled an exception: " + ex.Message);
}
}
不要怀疑代码,看看。
如果您需要,可以使用此辅助方法:
internal class CurrentDirectoryHelpers
{
internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct IISConfigurationData
{
public IntPtr pNativeApplication;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzFullApplicationPath;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzVirtualApplicationPath;
public bool fWindowsAuthEnabled;
public bool fBasicAuthEnabled;
public bool fAnonymousAuthEnable;
}
public static void SetCurrentDirectory()
{
try
{
// Check if physical path was provided by ANCM
var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
if (string.IsNullOrEmpty(sitePhysicalPath))
{
// Skip if not running ANCM InProcess
if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
{
return;
}
IISConfigurationData configurationData = default(IISConfigurationData);
if (http_get_application_properties(ref configurationData) != 0)
{
return;
}
sitePhysicalPath = configurationData.pwzFullApplicationPath;
}
Environment.CurrentDirectory = sitePhysicalPath;
}
catch
{
// ignore
}
}
}
当我开始我的服务时(API on .Net Core 2.2 in Docker container)我收到警告:
No XML encryptor configured. Key {daa53741-8295-4c9b-ae9c-e69b003f16fa} may be persisted to storage in unencrypted form.
我没有配置 DataProtection。我找到了配置 DataProtection 的解决方案,但我不需要保存此密钥。对我来说,如果密钥只会保留到应用程序重新启动 - 没关系。但是我不需要在日志中看到这个警告
有什么想法吗?我们该怎么做?
我的创业公司 Class 看起来像这样:
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services) {
services.AddMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddApiVersioning(o => o.ApiVersionReader = new HeaderApiVersionReader("api-version"));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseMvc();
lifetime.ApplicationStarted.Register(OnApplicationStarted);
lifetime.ApplicationStopping.Register(OnShutdown);
}
public void OnApplicationStarted() {
Console.Out.WriteLine($"Open Api Started");
}
public void OnShutdown() {
Console.Out.WriteLine($"Open Api is shutting down.");
}
}
也许它也有帮助我在项目中的包
<ItemGroup>
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.5" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.5.4" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.18.6" />
</ItemGroup>
这可能是权限错误,但您必须确定。抛出此错误时尝试登录。我看到在开发环境中出现此错误的原因有很多,通常是文件读取权限或找不到文件或没有文件。
用下面的日志记录算法包装你的主要功能,看看有什么问题:
public static void Main(string[] args)
{
CurrentDirectoryHelpers.SetCurrentDirectory();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Serilog", LogEventLevel.Information)
.WriteTo.File("Logs/LogFrom_ProgramMain.txt")
.CreateLogger();
try
{
var whb = WebHost.CreateDefaultBuilder(args).UseContentRoot(Directory.GetCurrentDirectory());
//whb... your codes
Log.Logger.Information("Information:blabla");
}
catch(Exception ex)
{
Log.Logger.Error("Main handled an exception: " + ex.Message);
}
}
不要怀疑代码,看看。
如果您需要,可以使用此辅助方法:
internal class CurrentDirectoryHelpers
{
internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct IISConfigurationData
{
public IntPtr pNativeApplication;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzFullApplicationPath;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzVirtualApplicationPath;
public bool fWindowsAuthEnabled;
public bool fBasicAuthEnabled;
public bool fAnonymousAuthEnable;
}
public static void SetCurrentDirectory()
{
try
{
// Check if physical path was provided by ANCM
var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
if (string.IsNullOrEmpty(sitePhysicalPath))
{
// Skip if not running ANCM InProcess
if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
{
return;
}
IISConfigurationData configurationData = default(IISConfigurationData);
if (http_get_application_properties(ref configurationData) != 0)
{
return;
}
sitePhysicalPath = configurationData.pwzFullApplicationPath;
}
Environment.CurrentDirectory = sitePhysicalPath;
}
catch
{
// ignore
}
}
}