Creating Sessions: How To?

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Hello,

I have been looking for some help/tutorial on how to create a session. What I want to do is allow a user to login, depending on the user - certain tables are displayed (or whatever). The closest I got to sombody telling me how to do this was to create a session. When I asked how to do it - I haven't received any response so I thought I would try here. What I was doing before, which I was told was completely wrong, was this:

Page 1:
VB.NET:
[/font]
[color=#0000ff][font=Arial][color=blue]Private[/color][color=blue]Sub[/color][color=#000000] btnLogin_Click([/color][color=blue]ByVal[/color][color=#000000] sender [/color][color=blue]As[/color][color=#000000] System.Object, [/color][color=blue]ByVal[/color][color=#000000] e [/color][color=blue]As[/color][color=#000000] System.EventArgs) [/color][color=blue]Handles[/color][color=#000000] btnLogin.Click[/color][/font][/color]
[color=#0000ff][font=Arial][color=blue][/color][/font][/color]
[color=#0000ff][font=Arial][color=blue]Dim[/color][color=#000000] User [/color][color=blue]As [/color][color=blue]String[/color][color=#000000] = txtUser.Text[/color][/font][/color]
[color=#0000ff][font=Arial][color=blue]Dim[/color][color=#000000] Pass [/color][color=blue]As [/color][color=blue]String[/color][color=#000000] = txtPassword.Text[/color][/font][/color]
[color=#0000ff][font=Arial][color=blue][/color][/font][/color]
[color=#0000ff][font=Arial][color=blue]If[/color][color=#000000] User = "" [/color][color=blue]Then[/color][/font][/color]
[color=#0000ff][font=Arial][color=#000000][/color][/font][/color]
[color=#0000ff][font=Arial][color=#000000]Response.Redirect("webform2.aspx?name=none")[/color][/font][/color]
[color=#0000ff][font=Arial][color=blue][/color][/font][/color]
[color=#0000ff][font=Arial][color=blue]Else[/color][/font][/color]
[color=#0000ff][font=Arial][color=#000000][/color][/font][/color]
[color=#0000ff][font=Arial][color=#000000]Response.Redirect("webform2.aspx?name=" & System.Web.HttpUtility.UrlEncode(User) & "")[/color][/font][/color]
[color=#0000ff][font=Arial][color=#000000][/color][/font][/color]
[color=#0000ff][font=Arial][color=blue]End [/color][color=blue]If[/color][/font][/color]
 
[color=#0000ff][font=Arial][color=blue]End [/color][color=blue]Sub[/color][/font][/color]
[color=#0000ff][font=Arial][color=#000000]
[/color]

Page 2
VB.NET:
[/color][/font]
[font=Arial][color=blue]Private[/color][color=blue]Sub[/color][color=#000000] Page_Load([/color][color=blue]ByVal[/color][color=#000000] sender [/color][color=blue]As[/color][color=#000000] System.Object, [/color][color=blue]ByVal[/color][color=#000000] e [/color][color=blue]As[/color][color=#000000] System.EventArgs) [/color][color=blue]Handles[/color][color=blue]MyBase[/color][color=#000000].Load[/color][/font]
 
[font=Arial][color=blue]If[/color][color=#000000] Request.QueryString("name") = "none" [/color][color=blue]Then[/color][/font]
 
[font=Arial][color=#000000]lblUserMessage.Text = "Not Logged In"[/color][/font]
 
[font=Arial][color=#000000]btnLogOut.Visible = [/color][color=blue]False[/color][/font]
 
[font=Arial][color=blue]Exit [/color][color=blue]Sub[/color][/font]
 
[font=Arial][color=blue]End [/color][color=blue]If[/color][/font]
 
[font=Arial][color=blue]If [/color][color=blue]Not[/color][color=#000000] (Page.IsPostBack) [/color][color=blue]Then[/color][/font]
 
[font=Arial][color=#000000]lblUserMessage.Text = "Logged In As: "[/color][/font]
 
[font=Arial][color=#000000]lblUser.Text = Request.QueryString("name")[/color][/font]
 
[font=Arial][color=blue]End [/color][color=blue]If[/color][/font]
 
[font=Arial][color=blue]End [/color][color=blue]Sub[/color][/font]
[font=Arial][color=blue]
As you can see - all that is happening here is I am passing one value from the first page - to the next page. I didn't include any code to acutally auth the user, this was just a test. Now I can do it this way and code accordingly to display different content for different users - but it would be a lot of work. I was told there is a much easier way to do it and it included creating a "Session". Also, my way - there isn't really a user "logged In" so you can't "Log Out" the user -

I am stuck, yet again. Can sombody help me out here.

Respectfully,

[/color]Scott
 
Last edited:

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
I changed page two to this and I like it - but again, I am not sure if I am going down the right path here. Let me know.

Page 2
VB.NET:
[/font]
[font=Arial][color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] Page_Load([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] [color=#0000ff]MyBase[/color].Load[/font]
[font=Arial][/font] 
[font=Arial][color=#0000ff]If[/color] Request.QueryString("name") = "none" [color=#0000ff]Then[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial]response.write("Not Logged In")[/font]
[font=Arial][/font] 
[font=Arial]btnLogOut.Visible = [color=#0000ff]False[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial][color=#0000ff]Exit[/color] [color=#0000ff]Sub[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial][color=#0000ff]End[/color] [color=#0000ff]If[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial][color=#0000ff]If[/color] [color=#0000ff]Not[/color] (Page.IsPostBack) [color=#0000ff]Then[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial]Session("user") = Request.QueryString("name")[/font]
[font=Arial][/font] 
[font=Arial]Response.Write(Session("user"))[/font]
[font=Arial][/font] 
[font=Arial][color=#0000ff]End[/color] [color=#0000ff]If[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial][color=#0000ff]End[/color] [size=3][color=#0000ff][size=2]Sub[/size][/color][/size][/font]
[font=Arial][color=#0000ff]
[/color]

The code above will write a line of Not Logged In if I click on the login button from Page 1 without a user, and if a user is entered - it will display that user. Below I coded to log the user out - but again, I am not sure if I am on the right path:
VB.NET:
[/color][/font]
[font=Arial][color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btnLogOut_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnLogOut.Click[/font]
[font=Arial][/font] 
[font=Arial]Session.Remove("user")[/font]
[font=Arial][/font] 
[font=Arial][color=#0000ff]If[/color] Session("user") = [color=#0000ff]Nothing[/color] [color=#0000ff]Then[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial]Response.Write("Logged Out")[/font]
[font=Arial][/font] 
[font=Arial][color=#0000ff]End[/color] [color=#0000ff]If[/color][/font]
[font=Arial][color=#0000ff][/color][/font] 
[font=Arial][color=#0000ff]End[/color] [size=3][color=#0000ff][size=2]Sub[/size][/color][/size][/font]
[font=Arial][color=#0000ff]
The code above will display "Logged Out" if I click the logout button - but did I really log the user out? Post your thoughts :)
 
Top Bottom