您好,我们有 C# 应用程序,它将 docker 映像 mcr.microsoft.com/dotnet/aspnet:8.0 部署到 azure kubernetes 服务。我们在 AKS 中有一个名为外部应用程序的服务,需要通过 http 连接到另一个服务,但它在 Azure 应用程序洞察中抛出异常“在密钥环中找不到密钥”,并且在同一个异常中它显示“取消保护会话时出错”饼干”
当我检查 Pod 时,它没有任何密钥环文件夹。我们还按照此处的文章部署了数据保护https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-8.0但它并没有采取密钥也来自我们的 blob 存储,并且我们不断收到“找不到密钥”
我的问题是钥匙从哪里来?看起来我们的数据保护实施尚未开始
我认为如果我们了解它们来自哪里,那么我们就能取得进展。请分享密钥实际来自哪里?
我基本上想知道,如果我们不尝试按照上面的文章存储在 blob 或 azure key Vault 中,默认情况下它是如何生成的
如果您不需要任何自定义存储,例如 Azure Blob 存储、Azure Key Vault 或 Redis。
ASP.NET Core
根据操作系统/环境选择默认密钥存储位置。
我创建了一个简单的应用程序,默认存储密钥,存储位置取决于运行时环境。
这是我的program.cs:
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddConsole();
builder.Services.AddDataProtection()
.SetApplicationName("DataProtectionDemo")
.AddKeyManagementOptions(options =>
{
options.NewKeyLifetime = TimeSpan.FromDays(90);
});
var app = builder.Build();
app.MapGet("/protect", (IDataProtectionProvider provider) =>
{
var protector = provider.CreateProtector("SamplePurpose");
var protectedData = protector.Protect("Hello World");
return $"Protected Data: {protectedData}";
});
app.MapGet("/", () => "Hello from Data Protection Demo!");
app.Run();
在上面的代码中,当使用 ASP.NET Core 的数据保护系统通过
"Hello from Data Protection Demo!"
端点访问时,字符串 /protect
将被加密。
要查找存储的密钥, 如果应用程序有权访问 Windows 注册表,则密钥将存储在
windows+R
并输入 regedit 按 EnterHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\4.0.30319.0\DataProtection
此处存储密钥。如果您在此处没有看到任何键,则意味着应用程序没有管理员权限来写入注册表。
你可以看到,因为我没有管理员权限,所以我在这里看不到密钥
在这些情况下,密钥通常存储在文件存储中。 你可以在下面的路径中找到你的钥匙
C:\Users\<YourUser>\AppData\Local\ASP.NET\DataProtection-Keys\