码迷,mamicode.com
首页 > 其他好文 > 详细

Cookie跨域读取和配置

时间:2015-12-22 10:22:42      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

public ActionResult Index()
        {
            string title = "Let Me Try";
            string show = Request.Cookies["GetValue"] == null ? "" : Request.Cookies["GetValue"].Value;
            ViewBag.Show = show;
            ViewBag.TitleShow = title;
            return View();
        }

        public ActionResult SetCookie()
        {
            HttpCookie cookie = new HttpCookie("GetValue");
            cookie.Value = "Hello!";
            cookie.Expires = DateTime.Now.AddSeconds(15);
            cookie.Domain = "www.yutest.com";
            Response.Cookies.Add(cookie);
            return Content(cookie.Value);
        }

发布两个Web 分别为WebA WebB 分别设置绑定信息端口号任意:

技术分享

本机测试的话 可以修改下C:\Windows\System32\drivers\etc下的host文件

比如:127.0.0.1   www.yutest.com

然后运行网站发现Cookie已经共享了。

早上看帖子的时候突然发现自己做单机做多了,有时候考虑到以后的扩充会预留下接口,比如保存用户状态的时候,Session存储的方式就是本地Cookie加服务器缓存,

这时候多台服务器读不到相同cookie..就更别说Session了。。。

在搭配下Nginx测试下: http://www.yutest.com:9002/Home/

upstream firstWeb {
    server www.yutest.com:9000 weight=2; 
    server www.yutest.com:9001 weight=2;
    }
    server {
        listen       9002;
        server_name  www.yutest.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
                 proxy_pass         http://firstWeb; 
                 #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
                 proxy_set_header   Host             $host; 
                 proxy_set_header   X-Real-IP        $remote_addr; 
                 proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
  }
}

Cookie跨域读取和配置

标签:

原文地址:http://www.cnblogs.com/ylsforever/p/5065703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!