我正在尝试使用 Machine.Encode 方法加密 asp.net 4.0 中的 cookie,但出现编译错误。错误位于“Response.Cookies.Add”行。什么是正确的方法?
错误 2 参数 1:无法从 'string' 转换为 'System.Web.HttpCookie' 错误 1 匹配 'System.Web.HttpCookieCollection.Add(System.Web.HttpCookie)' 的最佳重载方法有一些无效参数
public static string MachEncrypt (string plaintextValue)
{
var plaintextBytes = Encoding.UTF8.GetBytes (plaintextValue);
return MachineKey.Encode (plaintextBytes, MachineKeyProtection.All);
}
public static string MachDecrypt (string encryptedValue)
{
try
{
var decryptedBytes = MachineKey.Decode (encryptedValue, MachineKeyProtection.All);
return Encoding.UTF8.GetString (decryptedBytes);
}
catch
{
return null;
}
}
HttpCookie myCookie = new HttpCookie("co");
myCookie.Values.Add("customerId", dr["customerId"].ToString());
if (chkRemember.Checked)
{
myCookie.Expires = DateTime.Now.AddDays(30);
}
Response.Cookies.Add(StringEncryptor.MachEncrypt(myCookie.ToString()));