我收集数据并使用POST请求以下面的方式在服务器上设置cookie:
 HttpCookie cookies = new HttpCookie(name) { 
     Value = value, 
     Expires = DateTime.MaxValue, 
     Secure = true };
 HttpContext.Current.Response.Cookies.Remove(name);
 HttpContext.Current.Response.Cookies.Add(cookies);POST请求完成后,我使用js重新加载页面。我在该页面中使用了cookie中的一些值。
我试着让它们像这样:
if (HttpContext.Current.Request.Cookies[NameCookieName] != null)
{
     name = HttpContext.Current.Request.Cookies[NameCookieName].Value;
}
if (HttpContext.Current.Request.Cookies[RegionCookieName] != null)
{
     region = HttpContext.Current.Request.Cookies[RegionCookieName].Value;
}如果我使用HTTP,它工作得很好。
当我使用HTTPS cookies set,但当我读取它们时,coookie的值是NULL。
如何使用HTTPS设置/获取cookie?
发布于 2016-10-07 17:17:45
我认为你必须使用安全的财产。在HttpCookie类的C#中,有一个Secure属性,它允许您通过HTTPS获取和设置cookie。我相信您需要设置条件来设置和获取那些HTTPS页面以及HTTP页面上的cookie。
在HTTPS上设置的cookie应该只在HTTP上工作,所以可以使用Secure创建一个cookie。听起来像是因为没有设置cookie,所以默认情况下只在HTTP页面上设置cookie。
你可以参考下面的代码:
protected void Application_EndRequest(Object sender, EventArgs e)
    {
        string authCookie = FormsAuthentication.FormsCookieName;
        foreach (string sCookie in Response.Cookies)
        {
            if (sCookie.Equals(authCookie))
            {
                // Set the cookie to be secure. Browsers will send the cookie
                // only to pages requested with https
                var httpCookie = Response.Cookies[sCookie];
                if (httpCookie != null) httpCookie.Secure = true;
            }
        }
    }希望能对你有所帮助。
https://stackoverflow.com/questions/39913463
复制相似问题