实现原理: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;
        }

最新文章

  1. Leetcode_实现zigzag的转换_20161228
  2. ieee80211w
  3. poj2142-The Balance(扩展欧几里德算法)
  4. linux_iptables 详解
  5. mysql 1045的的解决方案
  6. 反射(学习整理)----Class类和加载器ClassLoader类的整理
  7. windows下搭建及配置mantis缺陷管理工具
  8. 顶级jQuery树插件
  9. mysql 基础之CURD
  10. xamarin android制作圆角边框
  11. 论文阅读——Visual inertial odometry using coupled nonlinear optimization
  12. [ZJOI2015]幻想乡战略游戏
  13. Multi-Projector Based Display Code ---- Download
  14. 自定义View的三个构造函数
  15. ASP.NET MVC+EF框架+EasyUI实现权限管理(附源码)
  16. Jenkins持续集成学习-Windows环境进行.Net开发3
  17. PHPActiveRecord validates
  18. 2018-2019-2 网络对抗技术 20165202 Exp2 后门原理与实践
  19. cp命令详解
  20. if else 的或(||)

热门文章

  1. sudo: /etc/sudoers is world writable
  2. Android应用开发SharedPreferences存储数据的使用方法
  3. linux git 推送空文件夹
  4. ICTCLAS 汉语词性标注集
  5. Visual Studio 2013 各个版本的产品密钥
  6. SqlServer 触发器
  7. js 实现进度条功能。
  8. C#中web页面之间传递数组参数
  9. 使用canvas元素-art方法绘制圆弧
  10. IOS懒加载