473,796 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Ben
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
Nov 18 '05 #1
8 3837
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("KeyNam e") = MyString
or
Session.Add("Ke yName", MyString )

To Retrieve:

Dim MyStoredString As String = CType(Session(" KeyName"), String)
or
Dim MyStoredString As String = CType(Session.I tem("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" <be******@nospa m.com> wrote in message
news:39******** *************** ***********@mic rosoft.com...
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

Nov 18 '05 #2
You can use the WebRequest and CookieCollectio n classes in the
System.Net namespace. CookieCollectio n 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,

--
Scott
http://www.OdeToCode.com

On Tue, 17 Feb 2004 06:06:10 -0800, Ben <be******@nospa m.com> wrote:
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


Nov 18 '05 #3
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" <be******@nospa m.com> a écrit dans le message de
news:39******** *************** ***********@mic rosoft.com...
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


Nov 18 '05 #4
Ben
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
Nov 18 '05 #5
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" <be******@nospa m.com> wrote in message
news:4E******** *************** ***********@mic rosoft.com...
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

Nov 18 '05 #6
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" <be******@nospa m.com> a écrit dans le message de
news:95******** *************** ***********@mic rosoft.com...
I should also mention that I am using the WebClient.Uploa dValues 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.
--Ben


Nov 18 '05 #7
Ben
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
Nov 18 '05 #8
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_Sessio nId". 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...ionstate.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/de...us/dv_vstechar
t/html/vbtchaspnetcook ies101.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

Nov 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4644
by: crjunk | last post by:
I've got an aspx page that allows the user to select different options whenever they want to run a report. On this page, I have added an additional text box for the user to enter in comments about the report they are running. I would like to be able to have the comments that the user has entered to appear on the report that is generated. I thought that I might be able to pass this text to the report by using a session variable but I...
0
1985
by: Bill | last post by:
If I have a webbrowser component in my app (created with studio 2003), and the user clicks a link which does a window.open from javascript, how can I create a new IE process and pass the session information to it? I'm guessing it is related to the pDisp variable on the component, but I'm not sure how to get the right object of the new IE process to stuff in there. Thanks for any hints or tips. -Bill ..NET framework 1.1
1
1504
by: aperez | last post by:
Hi, I need to pass a session variable from an ASP.NET v1.1 page to a v2.0 page, but haven't been able to do it. The reason is because I need to embed the 1.1 page in my intranet portal tool so I can ask it the user id that is currently logged in (using Plumtree, .. I mean BEA). The portal only supports v1.1. I want to pass that session variable to multiple pages externally that are running v2.0. Is this possible? Please help.
5
4017
by: manny6677 | last post by:
Trying a simple test of passing $_SESSION variables between two php pages. I don't see any data on the second page and the session id that is printed out is not the same as the first page session id of the same SESSIONSAMPLE name, which I think is the problem. When I refresh either page the session id constantly changes as well which doesn't seem right either. I have session.auto_start set to 0 (off). Any help would be greatly appreciated. ...
1
1176
by: clark | last post by:
Hi there, I want to wrap the following code in a function called ssGet() so I do not have to call isset() every time. //example db_var actually loaded dynamically $db_var = 2; if isset($_SESSION)) { $ret = $_SESSION
3
4766
by: hon123456 | last post by:
Dear all, I have a session variable session("loginid) which can be passed from A.asp to B.asp. Then In B.asp I have a hyperlink to C.asp e.g. < A href..C.asp>. The session variable cannot be passed to C.asp. The session variable is empty. What can I do to debug this problem? Thanks
1
2122
by: selvamsivalingam | last post by:
i call the popup window through javascript by using the below code. <script language="javascript"> function hpclick() { var WinSettings = "center:yes;resizable:yes;dialogHeight:300px" window.showModalDialog("http://localhost:4911/WebSite19/Create_category.aspx",WinSettings); } </script>
2
1939
by: gamernaveen | last post by:
Why do people pass session ids in URLs? (GET method) I mean whats the use? ex. sample.php?sid=<sessionid> Doesnt it work without passing ?
1
1309
by: ksrashmi | last post by:
HI How to track the session from Appache to tomcat . i.e From PHP to jsp .
13
10898
by: malcolmk | last post by:
Hi, been scratching my head and looking all over but cant get session data to pass to multiple pages. The scenario is like this. I present a splash page with an option to login at the bottom via form. Across the top is a menu strip, site access is open to all but if logged in and validatd as a member then selecting "downloads" or "articles" from the menu strip gives access to different content. I need my user to only login / validate once...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.