ASP.NET MVC饼干不节能? [英] ASP.NET MVC cookies not saving?

查看:124
本文介绍了ASP.NET MVC饼干不节能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有这个cookie在我的global.asax的在session_start方法救?

  //新用户匿名:

变种authCookie =新的HttpCookie(用户ID的String.Format({0} - {1},regiserAccountResponse.UserName,regiserAccountResponse.Key))
{
=到期DateTime.MaxValue,
区=domain.com
=安全真实,
仅Http = TRUE
};

//创建新用户的cookie - 有没有必要打电话给RegisterNewUserSession,因为这是在同一个呼叫
HttpContext.Current.Response.SetCookie(authCookie)完成的;


解决方案

您只需要如果要指定域限制cookie来你的网站的特定部分。该cookie将只包含在请求如果是在正确的范围。



通过设置域名为domain.com,你说小甜饼只提供给domain.com,这样你就不会从本地主机检测到它(或比domain.com以外的任何其他域)。



您还注意到,如果你试图从比你自己的,浏览器会斌它的其他域发送的浏览器的cookie。


Why doesn't this cookie save in the Session_Start method of my global.asax?

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

解决方案

You only need to specify the Domain if you want to limit the cookie to a specific section of your website. The cookie will only be included in the request if it is in the correct scope.

By setting the Domain to "domain.com", you are saying that the cookie will only be available to "domain.com", so you won't detect it from localhost (or from any other domain other than domain.com").

You will also notice that if you try and send a browser a cookie from a domain other than your own, the browser will bin it.

这篇关于ASP.NET MVC饼干不节能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆