Sessions - how do I get and pass my session id?

G

Guest

Hi there

I am writing a C# application that is making multiple POST requests to an ASP.NET page. I would like to take advantage of ASP.NET sessions to store some data on the server side. Where do I get my session id when I first hit the ASP page and how do I pass it back to the page so that ASP will recognize it

Thanks
Ben
 
S

S. Justin Gengo

Ben,

You use the session object (which is already created by the application for
you) like this:

To Add:

Dim MyString As String = "This is stored in session."
Session("KeyName") = MyString
or
Session.Add("KeyName", MyString )

To Retrieve:

Dim MyStoredString As String = CType(Session("KeyName"), String)
or
Dim MyStoredString As String = CType(Session.Item("KeyName"), String)

Note that I'm casting the object returned from the session because all items
stored in session variables are returned as Object.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


Ben said:
Hi there,

I am writing a C# application that is making multiple POST requests to an
ASP.NET page. I would like to take advantage of ASP.NET sessions to store
some data on the server side. Where do I get my session id when I first hit
the ASP page and how do I pass it back to the page so that ASP will
recognize it?
 
S

Scott Allen

You can use the WebRequest and CookieCollection classes in the
System.Net namespace. CookieCollection allows you to retrieve the
cookies and send them again with a later request. Do a search on the
class name and I'm sure you'll find some sample code.

Most sessions are implemented by sending a cookie to the client with
the session id, although there are such things as cookieless sessions.

HTH,
 
P

Patrice Scribe

This is done for you when using ASP.NET sessions...

You have just to store/retrieve session variables. ASP.NET will "connect"
each request with the appropriate set of session variables...

Patrice

--

Ben said:
Hi there,

I am writing a C# application that is making multiple POST requests to an
ASP.NET page. I would like to take advantage of ASP.NET sessions to store
some data on the server side. Where do I get my session id when I first hit
the ASP page and how do I pass it back to the page so that ASP will
recognize it?
 
G

Guest

Hi Justin,

Just using the Session object doesn't work because I get a new session object each time (I'm assuming this is because i'm not passing in a session Id in my request). I need to know how ASP.NET expects to be given the session id.

Thanks,
--Ben
 
S

S. Justin Gengo

Ben,

You shouldn't get a new sessionId each time. Sessions depend on cookies
being accepted on the client. If you need to be cookie independent then a
better method might be to store the data you need to persist using
viewstate.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


Ben said:
Hi Justin,

Just using the Session object doesn't work because I get a new session
object each time (I'm assuming this is because i'm not passing in a session
Id in my request). I need to know how ASP.NET expects to be given the
session id.
 
P

Patrice Scribe

Do you resend the session cookie each time ?
You may want to use the cookieless mode. In this mode, the session id is
passed on the querystring making it probably easier to test/debug.
Also I believe, that the session id is given a fixed value when you store a
session variable for the first time (ie you may have to use whatever comes
which each request rather than to keep the very first value).

At last I would really start with something as simple as possible :
- just have a page that set some session variables and displays the
sessionId, have a client displaying this value to see if it keeps changing

Good luck

Patrice

--

Ben said:
I should also mention that I am using the WebClient.UploadValues method to
make my request. I'm using it instead of the HttpWebRequest because passing
a fairly complex form and it's much easier to create the collection than to
format a multi-part form POST request.
I was kind of hoping some magic would happen and ASP would keep track of
the session without my having to do anything but every time I hit the page I
am being assigned a new session.
 
G

Guest

Ok I found out how to do it. For anyone else who is interested

The HttpWebRequest class has a CookieContainer property that is null by default. Create a new CookieContainer and assign it to the request. When you get the response you can call CookieContainer.GetCookies( requestUri ) and look through the cookies. The ASP session cookis is called "ASP _SessionId", the next time a request is made create a cookie container for it and add the session cookie and then ASP will recognize the session

--Ben
 
S

Steven Cheng[MSFT]

Hi Ben,


Thanks for posting here. I'm glad that you've figured out this problem
yourself. As you've found , the session (on the serverside) is associated
with client via a cookie variable named "ASP.NET_SessionId". If we want to
mantain the session outside , not via asp.net page request, we need to
manually retrieve the cookie variable from cookie collection and repost it
next time so as to maintain the association. In addition, as the other
customer has mentioned, if
the client doesn't support cookie, the ASP.NET serverside also provide a
cookie less session implmentation , which will put the session id as
encryped value in the url, thus, we can't use cookie variable to retrieve
the sessoinid. For detailed info on the ASP.NET session implemention, you
can view the following reference in MSDN:

#Underpinnings of the Session State Implementation in ASP.NET
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnetsessionstate.asp?f
rame=true

Also, here is a tech ariticle discussing the cookie in ASP.NET
#basics of cookies in asp.net
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchaspnetcookies101.asp

Hope they're also helpful to you.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top