SessionID

K

Kenny

Hi,

I have created an ASPX

Dim ss As HttpSessionState
ss = HttpContext.Current.Session
HttpContext.Current.Session("tesAt") = "testValue"
Response.Write(ss.SessionID() & "|<br>")
Response.Write(HttpContext.Current.Session("tesAt") & "|<br>")
Response.Write(HttpContext.Current.Session(ss.SessionID) & "|<br>")

Is there anyone know why I am not able to get the value of the session using
SessionID?

Thanks,
Kenny
 
H

Hans Kesting

Kenny said:
Hi,

I have created an ASPX

Dim ss As HttpSessionState
ss = HttpContext.Current.Session
HttpContext.Current.Session("tesAt") = "testValue"
Response.Write(ss.SessionID() & "|<br>")
Response.Write(HttpContext.Current.Session("tesAt") & "|<br>")
Response.Write(HttpContext.Current.Session(ss.SessionID) & "|<br>")

Is there anyone know why I am not able to get the value of the session using
SessionID?

Thanks,
Kenny

The first Response.Write line: I don't think you need the "()".
Second line: it should print "testValue". What do you get ?
Third line: what do you expect here? Your example didn't store anything
under that key.

Are you maybe trying to access other sessions than the current one?
That is not possible! (security reasons)
 
K

Kenny

Actually what I want to test out is
1) create a session with value and return the SessionID
3) use SessionID to retrieve back the value

How am I going to do it in ASP.NET? I think this doesnt break the security.

Thanks a lot,
Kenny
 
H

Hans Kesting

Kenny said:
Actually what I want to test out is
1) create a session with value and return the SessionID
3) use SessionID to retrieve back the value

How am I going to do it in ASP.NET? I think this doesnt break the security.

Thanks a lot,
Kenny

*you* are not creating a session, asp.net does.
HttpContext.Current.Session retrieves the session that
is associated with the current request. In your
aspx page you could also use the "Session" property
to retrieve exactly the same session.
There is *no* way to "retrieve a session" with some
specific session id.

The usual way to work with Session is to store values,
under specifc keys. Later (some other request) you can
retrieve the values using those keys (and maybe some casting).
See your "tesAt" example.

Note: in your code you seem to handle the "ss" variable
and "HttpContext.Current.Session" as if they were different
things. After the assignment in the second line (ss = ...)
they point to the *same* HttpSessionState object.
 
J

Juan T. Llibre

Hi, Kenny.

You are making things too difficult for yourself.

If you want to set a Session variable, use:
Session("test") = "My Test Session Content"

If you want to write a Session variable's value, use:
Response.Write(Session("test"))

or, if you want to retrieve it into a local variable, use :
Dim MyNewVariable as String = (Session("test"))
Now you could use the contents of Session("test") in code

To retrieve the SessionID, use :
Response.Write(Session.SessionID)

You don't create Sessions. ASP.NET creates them.
All you need to do is set and retrieve values.

It also look like you are confusing "writing an ASPX file"
with "writing the source codebehind code for an ASPX file",
which could be either "sample.aspx.vb" or "sample.aspx.cs".

If you want to work with HttpSessionState in codebehind, here's an example:

Dim MySession As HttpSessionState = Session.Contents

This assigns the current session-state object to the variable MySession.

Notice : that does *not* create a new session.

It allows you to retrieve the value of any
variable stored in the Session collection.

In an ASPX page, you'd only need to retrieve the value of a
Session.Contents item you set in global.asax's Session_OnStart :

in global.asax :
Sub Session_OnStart()
Session.Contents("AppStartTime") = Now
End Sub

in your aspx page:
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Response.Write(Session("AppStartTime"))
End Sub
</script>

HTH...



Juan T. Llibre
ASP.NET MVP
===========
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top