Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
'Create a session flag
Session("LoggedIn") = New Boolean
Session("LoggedIn") = False
End Sub
if the login page add this:
VB.NET:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If statement ... validating user was succeful
Session("LoggedIn") = True
End if
End Sub
then when user is logout set the Session("LoggedIn") to False and finally use the code from above (check the session and take an adequate action regard result)
Regards
P.S. It always works for me ... i have no idea why it doesn't work for you. If this suggestion is worthless then send me the global file and i will take a look at it ...
Thanks so much. I am running the painfully slow Norton Antivirus on my computer now. I'll try out your suggestions just as soon as its over. Thanks again.
Hi Kulrom, I did exactly as you said but it still doesn't work. Am I declaring these session state variables in the wrong place? Here are my code snippets:
sub page_load(sender As Object, e As System.EventArgs)
Dim conn as SqlConnection = New SqlConnection("server=(local);database=APMS;Trusted_Connection =yes")
Dim comm as SqlDataAdapter = new SqlDataAdapter("select * from UserData", conn)
Dim ds as new Dataset() comm.fill(ds,"Part")
end sub
sub cmd_login(sender As Object, e As System.EventArgs)
Dim conn as SqlConnection = New SqlConnection("server=(local);database=APMS;Trusted_Connection =yes")
Dim comm1 As SqlCommand = New SqlCommand("select * from UserData where UserID = '"& txtUserID.text &"' and Password = '"& txtPassword.text &"' ", conn)
Dim myReader As SqlDataReader Dim blnflag As Boolean = False
Conn.Open()
myReader = comm1.ExecuteReader
While myReader.Read()
Session("LoggedIn") = True
Session("UserName") = myreader.item("FName")
blnflag = True
response.redirect("franav.htm")
End While
If blnflag = False Then
lblMessage.Text = "Incorrect User ID or Password. Please try again."
End If
conn.close()
End Sub
fraNav is a frameset I use to hold my webforms. I put the folllowing code at the very top of the HTMl section, before the DOCTYPE, and html and head tags, like this:
<% If Session ("LoggedIn") = False Then
Response.Redirect("frmLogin.aspx")
End If
%>
At the top of the HTML section of frmHome.aspx, one of the webforms in fraNav, I have the same code.
At the top of the HTML Section of frmNav.aspx, another one of the webforms in fraNav, I have the same code. The logout hyperlink on this form has NavigateURL = "frmLogout.aspx" and Target ="_parent"
At the top of the frmLogout.aspx , again before the HTML tags, I have:
<% Session("LoggedIn") = False %>
After all this, when I click the Back button on my browser, I can still see the previous pages. This really sucks. Please tell me what I am doing wrong.
ncamoens... if I were you I would implement the out-of-the-box ASP.NET FormsAuthentication module. This way ASP.NET will block access to pages automatically if the user is not authenticated. Here's a website to get you started: http://www.15seconds.com/issue/020220.htm
FormsAuthentication means you will provide a web page where the user will login. There are alternative available approaches in ASP.NET and you don't have to figure out all the details yourself.. just implement their solution.
Note: You don't have to store the credentials in the web.config or a database unless you want to... my application requires logging in via a 3rd party application... after they login I use the FormsAuthentication module to create an encrypted FormsAuthentication cookie and ticket... works great.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.