cookie跨二级域名和顶级域名

1. 二级域名下  cookie跨域名
 Response.AddHeader("P3P", "CP=CAO PSA OUR");//ie浏览器下需写入
                HttpCookie cookie = new HttpCookie("username");
                cookie.Domain = ".baidu.com";//域名
                cookie.Expires = DateTime.Now.AddDays(14);
                cookie.Value =value.Name;
                Response.AppendCookie(cookie);

2.顶级域名的跨cookie用iframe嵌套或者用js调用接口

先写一接口 为sso.ashx 在a域名下

<%@ WebHandler Language="C#" Class="SSO" %>

using System;
using System.Web;

public class SSO : IHttpHandler {
    
    public void ProcessRequest (HttpContext context)
    {
        HttpRequest rq = context.Request;
        string username = "";
        if (!string.IsNullOrEmpty(Convert.ToString(rq.QueryString["username"])))
        {
            username = Convert.ToString(rq.QueryString["username"]);
        }
        
        HttpCookie cookie = new HttpCookie("username");
        cookie.Domain = "tt.com";
        cookie.Path = "/";
        cookie.Expires = DateTime.Now.AddMinutes(10000);
        cookie.Value = username;
        context.Response.Cookies.Add(cookie);

        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("P3P", "CP=CAO PSA OUR");
        context.Response.Write("");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    

}
js调用 b域名下 <script type="text/javascript" src="http://www.tt.com/SSO.ashx?username=1321"></script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值