Link to home
Start Free TrialLog in
Avatar of Ennio
Ennio

asked on

send user to the right page

I have two pages on my website that needs login, so if you go to the page and don't have the permission it will send you to the login page. Now on my login page I have hard code after the user logs in go to the index.aspx page, but since I'm using the same login page for all the password protected page how can I send the user to the last page he visited.
Avatar of laotzi2000
laotzi2000

In your login page, when you are authenticating the user, you can
get the url by:
FormsAuthentication.GetRedirectUrl(txtUsername.Text, False)

Then you can save this somewhere, and in index.aspx, redirect to the saved page.
Avatar of Ennio

ASKER

where should I add that?

should I add on my page load? or on my response.redirect??

here is part of the code

*******************

<script language="vb" runat="server">
Sub Page_Load (s As Object, e As EventArgs)handles mybase.load
      If Not isPostBack Then
            Session.Item("uname") = ""
      End If
End Sub

....

Sub btnSubmit_Click(sender As Object, e As EventArgs)
....
response.Redirect()
            Else
                  myConn.Close()
                  Label2.Text = "Invalid Username/Password"
            End If
It should be added right before response.redirect
Avatar of Ennio

ASKER

so it should be like this


   FormsAuthentication.GetRedirectUrl(txtUsername.Text, False)
   response.Redirect()
Else
    myConn.Close()
    Label2.Text = "Invalid Username/Password"
End If
Avatar of Ennio

ASKER

Hum I was looking and I think I'm doing something wrong here, here is the full code. I'm new to asp .net
am I doing this right?


******************************************************************************



<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script language="vb" runat="server">
Sub Page_Load (s As Object, e As EventArgs)handles mybase.load
      If Not isPostBack Then
            Session.Item("uname") = ""
      End If
End Sub

Sub btnSubmit_Click(sender As Object, e As EventArgs)
      If Page.IsValid Then
            Dim myConn As OleDbConnection
            Dim myComm As OleDbCommand
            myConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " & Server.MapPath("/database/babysitter.mdb") & ";")
            myConn.Open()      
            myComm = New OleDbCommand("Select * from tbl_babysitter where email='" & txtuname.text & "' and passwd='" & txtpwd.Text & "'", myConn)
            If (myComm.ExecuteReader().HasRows() ) then
                  Session.Add("uname", txtuname.text)
                  myConn.Close()
                  response.Redirect("index.aspx")
            Else
                  myConn.Close()
                  Label2.Text = "Invalid Username/Password"
            End If
      Else
            Label2.Text = "You must enter the required fields"
      End If
End Sub
</script>


</head>
<body>
<form runat="server">
  <asp:Label ID="Label2" runat="server"/><br />
  Username:
  <asp:TextBox ID="txtuname" TextMode="SingleLine" runat="server" />  
  <br>
  Password:
  <asp:TextBox ID="txtpwd" TextMode="Password" runat="server" />  
  <asp:Button ID="btnsubmit" runat="server" OnClick="btnSubmit_Click" Text="Ok" />
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of laotzi2000
laotzi2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you did not set the authentication cookie after authenticate successfully
Avatar of Ennio

ASKER

I think so, because in my other pages I just check for the Session.Item("uname") = to some data, and they get the permission to the page.

Avatar of Ennio

ASKER

Can some one give a help here on how to setup the login in the right way, please?
Avatar of Ennio

ASKER

I think i got it.. i let you know