实现Cookie跨域共享

摘要:
authenticationmode=“表单”>formsdomain=“abc.com”name=“abc.authcookie”protection=“无”/>strirncTicket=FormsAuthentication.Encrypt(票证);

实现原理:cookie是不能跨域访问的,但是在二级域名是可以共享cookie的

概念说明:站点1=a.abc.com   站点2=b.abc.com

实现步骤:1. 配置两个站点的webconfig

              2. a.abc.com写入cookie 

              3. b.abc.com读取cookie

一、配置Webconfig:

<httpRuntime targetFramework="4.0" />     

    我用的vs2012,默认生成的targetFramework=4.5 不知道为什么 4.5就不能跨域,有知道的朋友请指教。

<authentication mode="Forms">
     <forms domain="abc.com" name="abc.authcookie" protection="None" />
</authentication>

    测试了N久,这三个属性少一个都不能访问。两个站点的authentication配置是一样的。

二、站点1写入cookie

      //利用asp.net中的form验证加密数据,写入Cookie
      private HttpCookie GetAuthCookie(string userData, string userName)
     {

           //登录票证
           FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
              3,
              userName,
              DateTime.Now,
              DateTime.Now.AddMinutes(100000),
              false,
              userData,
              FormsAuthentication.FormsCookiePath  //可在webconfig中设置 默认为/
           );

           string encTicket = FormsAuthentication.Encrypt(ticket);

           if ((encTicket == null) || (encTicket.Length < 1))
          {
                throw new HttpException("Unable_to_encrypt_cookie_ticket");
          }
          
          HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
          cookie.Path = "/";
          cookie.HttpOnly = true;  //是否可通过脚本访问 设置为true 则不可通过脚本访问
          cookie.Domain = FormsAuthentication.CookieDomain;  //webconfig中设置的domain
          //cookie.Secure = FormsAuthentication.RequireSSL;  //当此属性为 true 时,该 Cookie 只能通过 https:// 请求来发送

          if (ticket.IsPersistent)     //票证是否持久存储
          {
              cookie.Expires = ticket.Expiration;
          }

          return cookie;
      }

三、站点2读取cookie

       T user=null;

       if (HttpContext.User != null
          && HttpContext.User.Identity.IsAuthenticated
          && HttpContext.User.Identity.Name != string.Empty
          && HttpContext.User.Identity.AuthenticationType == "Forms")
          {
                 FormsIdentity id = HttpContext.User.Identity as FormsIdentity;

                 if (id != null)
                {
                      FormsAuthenticationTicket ticket = id.Ticket;

                      user = this.DeserializeUserInfo(ticket.UserData);

                      if (user == null)
                      {
                          return false;
                      }

                      return true;

                }
                else
                {
                     user = default(user);

                     return false;
                }
        }
        else
        {
             user = default(user);

             return false;
        }

免责声明:文章转载自《实现Cookie跨域共享》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Arduino HX711使用LocalBroadcastManager 使用小解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF整理-使用用户选择主题的颜色和字体

“Sometimes it's useful to use one of the selected colors or fonts the user has chosen in theWindows Control Panel Personalization applet (or the older Display Settings in Windows...

js实现星空效果

本次主要是来实现上面的星空效果:在黑色的背景下面,有大小不一的星星在闪烁,当鼠标悬浮时,星星放大并旋转。 首先,我们需要一个大的夜空容器和放星星的容器: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> &l...

Openjudge1.11.1 查找最接近的元素

【描述】    在一个非降序列中,查找与给定值最接近的元素。【输入】    第一行包含一个整数n,为非降序列长度。1 <= n <= 100000。    第二行包含n个整数,为非降序列各元素。所有元素的大小均在0-1,000,000,000之间。    第三行包含一个整数m,为要询问的给定值个数。1 <= m <= 10000。 ...

[转帖]制作数据字典

  这部分内容和VB6的关系不大,但是确是困扰我的一个问题。 这几天在整理数据字典,以前的办法是用Excel来制作,一个一个的填写特别麻烦,制作到好说,关键就是一旦结果变更了,改来改去的麻烦死了。 后来改用数据库关系图来制作打印出来,看着还是那么回事情,但是表格的排序和查找太麻烦了,到底有没有好的办法呢? 当然有了,实际上在SqlServer中利用Sql语...

百度地图API初体验和偏移纠正方法

      最近的项目想做一个在可以通过手持设备获取经纬度,然后在地图上进行标注显示的功能,因为还在技术调研阶段,所以决定先使用百度地图或Google Maps的API来做Demo。通过网上的一些资料和自己对于Google和百度地图的使用,对这两个地图做了一些简单的对比,结论是很明显的——Google在技术水平和成熟度上都要比百度高很多,可以说完全不在一个...

HTTPUTILS

maven依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version>...