我正在尝试从头开始学习一些密码学,为此,我正在尝试测试都包含 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