我在下面的例子中遇到了cookie问题。 cookie确实被创建了,当我在创建后放置一个调试点时,我可以用手表查看cookie中的内容。但是,当我重新启动网站时,cookie仍然存在,但已变为空并且不会创建模型(所有字段为空都为空。)
我环顾四周,发现了与使用response.Cookie并没有过期日期有关的错误,但我改变了一切并保持空白。我做错了什么或是因为我使用localhost?
[HttpGet]
[Autorize]
public ActionResult ManagePaymentRun()
{
ViewData["currentAction"] = "Index";
payments.AccountNo = Request.Cookies["FSCSPayments"]["AccountNo"];
payments.SortCode = Request.Cookies["FSCSPayments"]["SortCode"];
payments.FirstChequeNo = "2";// Request.Cookies["FSCSPayments"]["FirstChequeNo"];
payments.FileName = Request.Cookies["FSCSPayments"]["FileName"];
payments.FRN = Request.Cookies["FSCSPayments"]["FRN"];
payments.JobNumber = Request.Cookies["FSCSPayments"]["JobNumber"];
payments.StartRecNo = Request.Cookies["FSCSPayments"]["StartRecNo"];
return View(payments);
}
internal void CreateCookie()
{
HttpCookie cookie = new HttpCookie("FSCSPayments");
cookie.Values.Add("AccountNo", payments.AccountNo);
cookie.Values.Add("SortCode", payments.SortCode);
cookie.Values.Add("FirstChequeNo", payments.FirstChequeNo);
cookie.Values.Add("FileName", payments.FileName);
cookie.Values.Add("FRN", payments.FRN);
cookie.Values.Add("JobNumber", payments.JobNumber);
cookie.Values.Add("StartRecNo", payments.StartRecNo);
cookie.Expires = DateTime.Now.AddDays(14);
cookie.Path = "/";
Request.Cookies.Add(cookie);
}
答案 0 :(得分:1)
cookie.Path = "C:\\Documents and Settings\\Andy\\Cookies";
这不是为了做你认为的事情。您无法指定cookie将存储在客户端计算机上的位置。这绝对取决于浏览器,你无法控制它。它是限制此cookie访问您网站的某些部分。因此,如果您不想要限制,只需将其设置为cookie.Path = "/"
。
此外,您的ManagePaymentRun
操作方法看起来很奇怪。为什么在[Autorize]
属性时测试用户是否经过身份验证:
[HttpGet]
[Authorize]
public ActionResult ManagePaymentRun()
{
ViewData["currentAction"] = "Index";
var payments = new Payments();
payments.AccountNo = Request.Cookies["FSCSPayments"]["AccountNo"];
payments.SortCode = Request.Cookies["FSCSPayments"]["SortCode"];
payments.FirstChequeNo = "2";// Request.Cookies["FSCSPayments"]["FirstChequeNo"];
payments.FileName = Request.Cookies["FSCSPayments"]["FileName"];
payments.FRN = Request.Cookies["FSCSPayments"]["FRN"];
payments.JobNumber = Request.Cookies["FSCSPayments"]["JobNumber"];
payments.StartRecNo = Request.Cookies["FSCSPayments"]["StartRecNo"];
return View(payments);
}
答案 1 :(得分:0)
我认为你应该解决这个问题,我有3个建议:
1)cookie.Expires = DateTime.Now.AddDays(14);
2)小心.Add(Key,Value),值应该是安全的,不包含一些符号,但我认为会引发异常
3)指定cookie.path
但我的赌注是第一个。
答案 2 :(得分:0)
这个问题已经解决,这是因为我使用的是IIS 5和MVC。我们现在将这个项目复制到IIS7 pc上,我们得到了cookie。