<system.web>
<machineKey validationKey="*********"
decryptionKey="*********"
validation="HMACSHA256"
decryption="AES" />
</system.web>
请问有人能够指导如何在ASP.NET Core 2.0中完成这个任务吗?
<system.web>
<machineKey validationKey="*********"
decryptionKey="*********"
validation="HMACSHA256"
decryption="AES" />
</system.web>
请问有人能够指导如何在ASP.NET Core 2.0中完成这个任务吗?
现在您需要使用DataProtection API:
ASP.NET Core数据保护堆栈提供了一个简单易用的加密API,开发人员可以用它来保护数据,包括密钥管理和轮换。
示例可在官方的DataProtection 存储库中找到。
顺便提一下,同样的方法也适用于ASP.NET:在ASP.NET中替换<machineKey>
数据保护系统基于两个核心概念——数据保护提供程序(由IDataProtectionProvider
接口表示),用于通过CreateProtector
方法创建数据保护器(由IDataProtector
接口表示)。数据保护器用于加密和解密数据。
要将IDataProtectionProvider
注册到DI中,请使用.AddDataProtection
方法:
public void ConfigureServices(IServiceCollection services)
{
// Adds data protection services
services.AddDataProtection();
...
}
services.AddDataProtection().ProtectKeysWithDpapi();
。 - codeMonkeypublic class MyContext : IDataProtectionKeyContext
{
...
// This maps to the table that stores keys.
public DbSet<DataProtectionKey> DataProtectionKeys { get; set; }
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDataProtection().PersistKeysToDbContext<MyContext>();
}
GenerateEmailConfirmationTokenAsync
而出现此错误,您需要设置应用程序名称:https://learn.microsoft.com/zh-cn/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-3.1#setapplicationname-1 - g4j如果您有遗留问题需要构建基于经典ASP.NET应用程序生成身份验证Cookie的ASP.NET Core应用程序,则有一个开源库可用,可让您将这些遗留Cookie集成到您的ASP.NET Core应用程序中。开发人员使用.NET Framework参考实现来构建自己基于Machinekey的加密/解密逻辑。请参见https://github.com/synercoder/FormsAuthentication
回复晚了,但对于任何愿意在ASP.net core中解密和加密FormsAuthentication票证令牌的人来说非常有用。这里有一个专门做这个的库:https://github.com/dazinator/AspNetCore.LegacyAuthCookieCompat
复制您的验证密钥和解密密钥,并将其作为参数传递给库的解密或加密函数。 请注意,如果解密或加密不起作用,请尝试使用加密算法和兼容性标志来匹配您的目的。例如:
var encryptor = new LegacyFormsAuthenticationTicketEncryptor(SHA512DecryptionKey, SHA512ValidationKey, ShaVersion.Sha256, CompatibilityMode.Framework20SP2);
或者更改标志和兼容性为:
var encryptor = new LegacyFormsAuthenticationTicketEncryptor(SHA512DecryptionKey, SHA512ValidationKey, ShaVersion.Sha512, CompatibilityMode.Framework45);
然后,按照下面所示的方式解密令牌:
FormsAuthenticationTicket result = encryptor.DecryptCookie(encryptedText);