ASP.NET MVC cookie不能保存?

6 c# cookies asp.net-mvc

为什么这个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);
Run Code Online (Sandbox Code Playgroud)

Fen*_*ton 5

如果要将cookie限制为网站的特定部分,则只需指定域.只有在正确的范围内,cookie才会包含在请求中.

通过将域设置为"domain.com",您说该cookie仅对"domain.com"可用,因此您不会从localhost(或来自domain.com以外的任何其他域)检测到它. .

您还会注意到,如果您尝试从您自己的域以外的域向浏览器发送cookie,浏览器将对其进行分类.