实现Cookie跨域共享
实现原理: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跨域共享的更多相关文章
- cookie跨域共享
domain和path属性,domain就是当前域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net,path默认就是当 ...
- [php] cookie 跨域共享
<?php // m.hinabian.com class Controller_Cookie extends Hnb_Base_Controller { public function ini ...
- 在ExpressJS中设置二级域名跨域共享Cookie
问题:我使用expressjs和mongostore来管理session.下面是expressjs中的设置. app.configure(function(){ app.use(express.ses ...
- cookie用法小结 cookie.setPath 跨域共享
1. JSP中Cookie的读写 Cookie的本质是一个键值对,当浏览器访问web服务器的时候写入在客户端机器上,里面记录一些信息.Cookie还有一些附加信息,比如域名.有效时间.注释等等. 下面 ...
- 跨域共享cookie和跨域共享session
转载自:http://blog.csdn.net/ahhsxy/article/details/7356128 这里所说的跨域,是指跨二级域名,而且这些域名对应的应用都在同一个app上, 比如我有以下 ...
- 跨域共享cookie
1. JSP中Cookie的读写 Cookie的本质是一个键值对,当浏览器访问web服务器的时候写入在客户端机器上,里面记录一些信息.Cookie还有一些附加信息,比如域名.有效时间.注释等等. 下面 ...
- cookie 跨域访问的解决方案
Cookie 同域单点登录 最近在做一个单点登录的系统整合项目,之前我们使用控件实现单点登录(以后可以介绍一下).但现在为了满足客户需求,在不使用控件情况下实现单点登录,先来介绍一下单点登录. ...
- session跨域共享解决方案
要让session跨域共享,需要解决三个问题: 1.通过什么方法来传递session_id? 2.通过什么方法来保存session信息? 3.通过什么方法来进行跨域? 一.传递session_id有4 ...
- 关于Cookie跨域操作的一些总结
正常的cookie只能在一个应用中共享,即一个cookie只能由创建它的应用获得. 1.可在同一应用服务器内共享方法:设置cookie.setPath("/"); 本机to ...
随机推荐
- fushioncharts 使用教程要点---使用JSON数据方式
1.建立图表步骤: A.下载fushionChart,引入FusionCharts.js和FusionChartsExportComponent.js文件 B.建立图表对象 var chart1 = ...
- RabbitMQ - TcpConnection析构引发的一次handshake_timeout
使用RabbitMQ时,连接rabbit-server一直连接失败,代码没有任何错误提示.但是通过rabbitmqctl始终查询不到连接以及创建的queue等信息. 官方的文件demo里面也没有Tcp ...
- 使用 itext、flying-saucer 实现html转PDF(转)
转自 http://blog.csdn.net/mhouwei62/article/details/51394804 表示感谢 itext-2.1.7高于此版本已经更新为AGPL授权,不建议使用. 添 ...
- Activity的Launch Mode
ANDROID四种启动模式: 1.standard:默认的启动模式,每次新建一个实例对象. 2.singleTop:如果在任务栈顶发现了相同的实例则复用该实例,否则新建一个实例并压入栈顶. 3.sin ...
- Lae程序员小漫画(三),仅供一乐
Lae软件开发,快乐程序员!
- UE4 VR 模式画面扭曲 解决方法
后期处理盒子 详细设置->setting->Misc->screen percentage 设置为100
- throw与throws的区别
throws语句 throws总是出现在一个函数头中,用来标明该成员函数可能抛出的各种异常.对大多数Exception子类来说,Java 编译器会强迫你声明在一个成员函数中抛出的异常的 ...
- Laravel框架数据库CURD操作、连贯操作使用方法
Laravel框架数据库CURD操作.连贯如何来操作了这个操作性是非常的方便简单了我们在这里来为各位介绍一篇相关的教程,具体的细节步骤如下文介绍. Laravel是一套简洁.优雅的PHP Web开 ...
- Tomcat报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
Failed to start component [StandardEngine[Catalina].StandardHost[localhost]] 解决办法: 1,检测你的web.xml.去掉所 ...
- Python图片处理
Python图像处理库PIL基本使用 #将图片转换为灰度图像 from PIL import Image pil_im = Image.open('cat.jpg') gray_cat = pil_i ...