跨域讀取Cookie和session之HttpWebRequest另類方法(網(wǎng)站API開發(fā)
時間:2009-10-25 14:58來源:未知 作者:admin 點擊:
880次
在網(wǎng)上找了很多跨域讀取Cookie的方法,但都是A域主動設(shè)置B域的Cookie,而沒有B域去獲取A域Cookie的方法。 所謂A域主動設(shè)置B域的Cookie 1:在B.com上新建一文件:SetCookie.aspx protected void Page_Load( object sender, EventArgs e) { HttpContext.Curren
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)在網(wǎng)上找了很多跨域讀取Cookie的方法,但都是A域主動設(shè)置B域的Cookie,而沒有B域去獲取A域Cookie的方法。
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)所謂A域主動設(shè)置B域的Cookie
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)1:在B.com上新建一文件:SetCookie.aspx
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)1. protected void Page_Load(object sender, EventArgs e)
2. {
3. HttpContext.Current.Response.AddHeader("p3p", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
4. HttpCookie cookie = new HttpCookie("userid", "44");
5. cookie.Domain = ".b.com";
6.
7. // cookie.Expires =
DateTime.Now.AddSeconds((double)expires);
8. HttpContext.Current.Response.AppendCookie(cookie);
9. }
2:在A域新建一文件:Default.aspx,在前臺頁面調(diào)用B域的SetCookie.aspx頁面,來為B域設(shè)置相應(yīng)的Cookie.
1. <script src="http://www.b.com/SetCookie.aspx"></script> 3:在B域新建一文件:Default.aspx來顯示被A域設(shè)置的Cookie。
1. protected void Page_Load(object sender, EventArgs e)
2. {
3.
4. Response.Write(Request.Cookies["userid"] == null ? "" : Request.Cookies["userid"].Value.ToString());
5.
6. }
4:以此訪問www.a.com/default.aspx---->www.b.com/default.aspx
以上為A域主動設(shè)置B域的Cookie,適用于單點登錄,但必須在B域,C域或D域上新建setcookie.aspx文件來讓A域幫忙設(shè)置Cookie。
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)那B域C域或D域如何根據(jù)自身的需要去主動獲取A域的Cookie呢?請看以下方法,以下方法為此文重點。
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)1:在A域新建一文件:SetCookie.aspx,此文件用來設(shè)置A域自己的Cookie。
1. protected void Page_Load(object sender, EventArgs e)
2. {
3. HttpCookie cookie = new HttpCookie("userid", "44");
4. HttpContext.Current.Response.AppendCookie(cookie);
5. }
2:A域的Cookie設(shè)置完了,那怎么讓其他域來讀取自己的Cookie呢,這就是重點了。
新建一頁面:OpenID.aspx,用來讓B域讀取Cookie,并自動設(shè)置B域的cookie。(這里有點昏)
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)3:A域的工作已經(jīng)完了,那B域如何得到A域的這個Cookie值呢,在B域新建一頁:default.aspx
3:調(diào)用B域時,發(fā)現(xiàn)沒有cookie就自動從A域獲取cookie值來設(shè)置自己的Cookie。(責(zé)任編輯:admin)
A域可以把怎么讀取的方法做成dll,提供給其他域。這稱之為A域的API。
測試成功,用此方法能跨域讀取session,方式是:把A域的Session值存入B域的cookie中。至于怎么存,存在什么cookie名稱里,由A域提供。
1. protected void Page_Load(object sender, EventArgs e)
2. {
3. HttpContext.Current.Response.AddHeader("p3p", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
4. if (Request.Cookies["userid"] != null)
5. {
6. Response.Write(@"
7. var userid=" +
Request.Cookies["userid"].Value.ToString() + @";
8. SetCookie('userid',userid);//什么域調(diào)用此文件,設(shè)置的Cookie將是什么域的。
9. window.location.href=document.URL;
10.
11. function SetCookie(name,value)//兩個參數(shù),一個是cookie的名子,一個是值
12. {
13. document.cookie = name + ""=""+ escape (value) ;
14. }
15. ");
16. }
17. else
18. {
19. Response.Write("document.write('沒有cookie');");
20. }
21. }
http://szrdedu.com我愛學(xué)習(xí)網(wǎng)1. protected void Page_Load(object sender, EventArgs e)
2. {
3. if (HttpContext.Current.Request.Cookies["userid"] != null)
4. {
5. Label1.Text =
HttpContext.Current.Request.Cookies["userid"].Value.ToString(); 6.
7. }
8. else
9. {
10.
11. Response.Write((責(zé)任編輯:admin)
12. " <script src=\"http://www.a.com/openid.aspx\"></script>");
13.
14. }
15. }
16.
4:依次訪問 www.a.com/setcookie.aspx ------>
www.b.com/default.aspx 此方法用于A域開發(fā)API給其他域調(diào)用。主要用于A域API接口的開發(fā)。就像現(xiàn)在的SNS網(wǎng)站提供當(dāng)前登錄的用戶信息給其他應(yīng)用程序(如搶車位)一樣,不過我不知道他們是怎么實現(xiàn)的,但我用此方法實現(xiàn)了。
總結(jié)下此方法的步驟:
1:A域設(shè)置cookie
2:B域用調(diào)用javascript腳本的方式讀取A域cookie,讀取后,并同步設(shè)置B域Cookie.