跨域cookie访问 Easy Cross Domain Cookies (Sharing cookies between domains)
2009-02-10 22:30
405 查看
跨域cookie访问
Easy Cross Domain Cookies (Sharing cookies between domains)
I own several websites that need memberships to post comments, and recently I wanted the ability to have a single login - so once the user is logged into one site they are automatically logged into the others.
Ideally, I could just write the login cookies for both domains from one location, or somehow share the same login cookie between the domains - but you quickly come up against browser security which (for good reason) doesn’t allow this sort of thing.
There are plenty of ways of solving this problem - but this is the simplest I could find. It uses an IFrame to set cookies on the foreign domain.
In the example, you have Domain A which the visitor is currently on, and Domain B, which you want to set cookies on.
Add a page ‘FrameLogin.aspx’ to your Domain B website.
[/code]
Note that the p3p header has been added, this is to allow 3rd party cookies.
To call this page, we insert code into the appropriate ‘Login’ function in your Domain A website.
[/code]
Once the cookie has been set on Domain B I can use it to auto-login my visitors when they get there.
Also: For simplicity, I am using an IFrame to call a page. If I wanted faster execution I could substitute an iHttpHandler for the page.
This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!
Related posts:
Copying cookies across domains in ASP.Net
We Got Domain - over 10,000 aged domains for sale
How to set third-party cookies with iframe Facebook Applications
Free PR2 domains (and one PR3)
Opening a new browser window with POST data
Easy Cross Domain Cookies (Sharing cookies between domains)
I own several websites that need memberships to post comments, and recently I wanted the ability to have a single login - so once the user is logged into one site they are automatically logged into the others.
Ideally, I could just write the login cookies for both domains from one location, or somehow share the same login cookie between the domains - but you quickly come up against browser security which (for good reason) doesn’t allow this sort of thing.
There are plenty of ways of solving this problem - but this is the simplest I could find. It uses an IFrame to set cookies on the foreign domain.
In the example, you have Domain A which the visitor is currently on, and Domain B, which you want to set cookies on.
Add a page ‘FrameLogin.aspx’ to your Domain B website.
Public Partial Class FrameLogin Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim sUserID As String HttpContext.Current.Response.AddHeader(”p3p”, “CP=\”"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\”"”) sUserID = Request.QueryString(”userid”) Dim oCookie As New HttpCookie(”UserID”) oCookie.Value = sUserID oCookie.Expires = DateTime.Now.AddDays(1000) HttpContext.Current.Response.Cookies.Add(oCookie) oCookie = Nothing End Sub End Class
[/code]
Note that the p3p header has been added, this is to allow 3rd party cookies.
To call this page, we insert code into the appropriate ‘Login’ function in your Domain A website.
Private Function Login(ByVal sUserName As String, ByVal sPassword As String) As Boolean Dim iUserID As Integer iUserID = CheckLoginOKAndGetUserdID(sUserName, sPassword) If iUserID <> 0 Then Login = True Response.Write(”<IFRAME style=’WIDTH:1px;HEIGHT:1px’ src=’http://www.DomainB.com/FrameLogin.aspx’ frameBorder=’0′></IFRAME>”) Else Login = False End If End Function
[/code]
Once the cookie has been set on Domain B I can use it to auto-login my visitors when they get there.
Also: For simplicity, I am using an IFrame to call a page. If I wanted faster execution I could substitute an iHttpHandler for the page.
This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!
Related posts:
Copying cookies across domains in ASP.Net
We Got Domain - over 10,000 aged domains for sale
How to set third-party cookies with iframe Facebook Applications
Free PR2 domains (and one PR3)
Opening a new browser window with POST data
相关文章推荐
- 跨域(cross-domain)访问 cookie (读取和设置)
- 跨域(cross-domain)访问 cookie (读取和设置)
- 跨域(cross-domain)访问 cookie (读取和设置)
- 跨域(cross-domain)访问 cookie (读取和设置)
- 跨域(cross-domain)访问 cookie (读取和设置)(http://www.dup2.org/node/384)
- P3P 和 跨域 (cross-domain) cookie 访问(读取和设置)
- 跨域(cross-domain)访问 cookie (读取和设置)
- Sharing cookie Across domain 跨域cookie访问 cookie跨域
- 跨域(cross-domain)访问 cookie (读取和设置)
- P3P 和 跨域 (cross-domain) cookie 访问(读取和设置)
- P3P 和 跨域 (cross-domain) cookie 访问(读取和设置)
- Flex快速入门系列之五:crossdomain.xml文件创建以及允许本地跨域访问远程资源
- [ Silverlight]crossdomain.xml 解决 Silverlight 跨域访问的问题
- python cherrypy RESTful API,cherrypy支持跨域ajax访问,CROS,crossdomain ajax
- iframe跨域访问cookie问题, A站iframe引用其它站(B站)的内容时,B站的页面获取不到B站种下的Cookies。
- flash跨域访问crossdomain.xml
- Spring 3.1 MVC REST 支持之跨域访问(Cross-origin resource sharing)
- 跨域文件crossdomain.xml在weblogic上的部署和http://10.165.26.151:7001/xx.xml可以访问一个文件
- flash 跨域访问的控制文件crossdomain.xml
- 浅谈cookie跨域的解决方案——document.domain