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()));