1

Here i am passing the session id on a button click from one asp.net application to another asp.net application.

Application 1:

protected void imgBTN_Click(object sender, EventArgs e)
{
   string sessionKey = HttpContext.Current.Session.SessionID;
   HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://localhost:43392/PartnerHome.aspx");
   string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(sessionKey));
   req.Headers.Add("Authorization", "Basic " + svcCredentials);
   try
   {
      using (WebResponse svcResponse = (HttpWebResponse)req.GetResponse())
      {
         using (StreamReader sr = new StreamReader(svcResponse.GetResponseStream()))
         {
            string jsonTxt = sr.ReadToEnd();
         }
      }
   }
   catch (Exception c)
   {
   }
}

and my problem here is how to retrieve this session id there in my second asp.net application pageload

Application 2:

protected void Page_Load(object sender, EventArgs e)
{
}

Any Suggestion?

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
kk1076
  • 1,740
  • 13
  • 45
  • 76
  • 1
    My first question is, why? – Lloyd Jan 10 '13 at 12:43
  • @Lloyd To share the same session id for both application – kk1076 Jan 10 '13 at 12:45
  • move session out of proc, for example to sql server and then look at this post http://stackoverflow.com/a/3151315/351383 – Antonio Bakula Jan 10 '13 at 12:50
  • @AntonioBakula http://stackoverflow.com/questions/14254737/share-same-sessionid-between-two-asp-net-web-applications. I am trying using the sql server mode and for the second application, I am getting a new session id in the ASPState database. – kk1076 Jan 10 '13 at 12:58
  • Why do you need it the same? – Lloyd Jan 10 '13 at 13:26
  • @Lloyd If both applications share same session, session timeout of one application should also expire the session in second application . – kk1076 Jan 10 '13 at 13:42
  • So, what you want is for the both sites' sessions to timeout at the same time? Or is it authentication timeout you are looking for (not the same thing...)? – mortb Jan 10 '13 at 13:54
  • @kk1076 are the two applications on different servers? For multiple servers to access the same session, they will need the same machine key (set in machine.config). – Graham Clark Jan 10 '13 at 14:20
  • @GrahamClark yes two applications are in different servers. i used the machine key in web.config. refer http://stackoverflow.com/questions/14254737/share-same-sessionid-between-two-asp-net-web-applications#comment19782278_14254737 – kk1076 Jan 11 '13 at 04:57
  • @mortb both sites, session timeout has to be the same. – kk1076 Jan 11 '13 at 06:15

1 Answers1

0

Another solution:

What I would do is to add an invisible image (some transparant pixel) on the page of application 1:

<img src="http://site2.domain.com/transpimage.aspx">

The invisible image would actually be a asp.net page hosted by site 2.
In the page load event of transpimage.aspx. I would output some image:

public void Page_Load(...)
{
     using(Bitmap image = new Bitmap(1,1))
     using(MemoryStream stream = new MemoryStream())
     {
          image.Save(stream);
          Response.ContentType = "image/png";
          stream.WriteTo(Response.OutputStream);
          Response.End();
     }
}

Since the image from application 2 is served by an aspx page (not a static image in a file) it will keep the session of application2 alive. I've done this before and it tends to be fairly robust.

If you are familiar with HttpHandlers you can write the "image page" as a handler, but don't forget that if you do it as a handler you need to inherit from the IRequiresSessionState interface to actually acccess the session.

Hope this helps!

mortb
  • 9,361
  • 3
  • 26
  • 44