[英]ASP.NET MVC cookies not saving?
为什么这个cookie没有保存在我的global.asax的Session_Start方法中?
//new anon user:
var authCookie = new HttpCookie("user-id", string.Format("{0}-{1}", regiserAccountResponse.UserName, regiserAccountResponse.Key))
{
Expires = DateTime.MaxValue,
Domain = "domain.com",
Secure = true,
HttpOnly = true
};
//create the new users cookie - there's no need to call RegisterNewUserSession as this is done in the same call
HttpContext.Current.Response.SetCookie(authCookie);
如果要将cookie限制为网站的特定部分,则只需指定域。 只有在正确的范围内,cookie才会包含在请求中。
通过将域设置为“domain.com”,您说该cookie仅对“domain.com”可用,因此您不会从localhost(或来自domain.com以外的任何其他域)检测到它。 。
您还会注意到,如果您尝试从您自己的域以外的域向浏览器发送cookie,浏览器将对其进行分类。
iOS现在非常热衷于捆绑无域cookie,所以虽然接受的答案说“如果你想将cookie限制在你网站的特定部分,你只需要指定域名” - 我不认为这是真的。 您需要设置域名。
以下代码段将从本地到生产:
private static HttpCookie CreateCookie(string name, string value)
{
return new HttpCookie(name, value) { Path = "/", Expires = DateTime.UtcNow.AddYears(1), Domain = Request.Url.Host };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.