在ASP .NET Webforms中,MachineKey(向导)是数据库安全性和应用程序安全性的重要组成部分。它用作用于表单身份验证的密码的加密密钥,因此,如果更改了机器密钥,将不再识别现有密码。这确实为将数据库中的密码存储链接到Web服务器上的令牌提供了一定程度的安全性。如果有多个使用单个数据库身份验证存储的Web服务器,则它们必须具有相同的计算机ID。
在.NET Core MVC中,我们现在有几个在Startup.cs
中声明的服务:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options => {
options.ExpireTimeSpan = new TimeSpan(0, 45, 0);
options.LoginPath = new PathString("/Account/Login");
options.AccessDeniedPath = new PathString("/Account/AccessDenied");
}
);
services.AddDataProtection()
.SetApplicationName("MyApplicationName")
.PersistKeysToFileSystem(new System.IO.DirectoryInfo(@"C:\server_config\my-app-keys"));
我已经查看了每种服务的方法,但都没有提供数据存储加密密钥。我以为ApplicationName可以用作密钥,但是如果我更改它,我仍然可以使用旧密码登录,因此显然不用于加密。
本文规定隔离是API的关键要求之一:https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-3.0
这是一个简单的示例,使用“目的字符串”在不同逻辑存储之间提供加密隔离:https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/using-data-protection?view=aspnetcore-3.0
但是我似乎无法将它们组合在一起以将.NET Core中的密码加密与特定的密钥联系在一起。
我想念什么?
如果我没记错的话,可以使用以下命令进行此操作:
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.AspNet.DataProtection.Repositories;
namespace MySpace
{
public class MyXmlRepository : IXmlRepository
{
public MyXmlRepository()
{
// Whatever I wanted injected in.
}
public IReadOnlyCollection<XElement> GetAllElements()
{
return null;
}
public void StoreElement(XElement element, string friendlyName)
{
// Persist something
}
}
}
如果您在IOC中注册,它将开始将其用于密钥持久性,然后您就可以做任何您想做的事。