0

Let say I have two domains(A,B). Domain A page is redirecting his page to a Domain B page. But before redirecting Domain A page need to set a cookie. I have tried this code on domain A page but it is not working,

var cookie = new HttpCookie("ID", id);
Response.Cookies.Add(cookie);
Response.Redirect("Domain B page");

Update: From my understanding the ID cookie is saving in Domain A instead of Domain B.

Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
  • I think it's your browser which forbids this to prevent malicious websites to do such things. I may be wrong but I think that's it :/ – Flinth Jul 31 '12 at 07:43
  • possible duplicate of [Why can't I set a cookie and redirect?](http://stackoverflow.com/questions/1621499/why-cant-i-set-a-cookie-and-redirect) – Shai Jul 31 '12 at 07:45
  • @Shai, How can you say this is a possible duplicate? I am talking about remote domain. – Imran Qadir Baksh - Baloch Jul 31 '12 at 07:57
  • I think there's a problem setting a cookie in such way, you gotta test it manually, see [this](http://stackoverflow.com/questions/4801604/setting-a-cookie-for-another-domain-being-302-redirected-to) – Shai Jul 31 '12 at 07:59
  • @user960567, when response is redirected the current thread is aborted and a new 302/301 status is sent to client to make a new request for the specified destination, so the thread that is settings cookie never gets completed, so instead of `Response.Redirect` use javascript on client to redirect e.g `window.location.href='Domain B URL'` – Furqan Hameedi Jul 31 '12 at 08:35
  • @Furqan, Please test with fiddler/wireshark/DevTool/FireBug – Imran Qadir Baksh - Baloch Jul 31 '12 at 08:41

1 Answers1

2

The pattern for doing things like this is to have a page on domain B that can set the cookie based on request parameters you have sent, then redirect to "Domain B page", which is also a parameter (in case you need flexibility).

The problem is you need to find a way to make sure that this page in domain B can't be used by an attacker to write bogus cookies.

linkerro
  • 5,318
  • 3
  • 25
  • 29