当前上下文中不存在计算机密钥



我正在尝试从头开始学习一些密码学,为此,我正在尝试测试都包含 MachineKey 类的保护和取消保护方法。

但是当将代码放入 asp.net 控制台应用程序中时,出现以下错误

"machinekey does not occur in the current context"

,即使尝试包含

using System.Web.Security

我做错了什么?我只想测试这两种方法,因此将它们放在控制台应用程序中,在那里我可以轻松检查输出。

保护方法的代码段:

public static string Protect(string unprotectedText)
{
var unprotectedBytes = Encoding.UTF8.GetBytes(unprotectedText);
var protectedBytes = MachineKey.Protect(unprotectedBytes, "Recipient: user");
var protectedText = Convert.ToBase64String(protectedBytes);
return protectedText;
}

Microsoft.AspNetCore.DataProtection.SystemWeb安装了软件包。

参考: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-2.2#package-layout

最新更新