473,466 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RedirectFromLoginPage

Hi,

I like using RedirectFromLoginPage because it redirects back to the
originally requested page after successful login. However, if the
originally requested page was the login form then it automatically redirects
to default.aspx.

How can I preserve this redirection behaviour but specify a different page
to redirect to (other than default.aspx)?

Thanks in advance.
Nov 21 '05 #1
3 2046
I think you can do this in the IIS Manager, select the site and then remove
everything but your chosen page. You may also have to add this to the filter
which allows n types of extensions through

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"rufus" <he***@online.de> wrote in message news:ci**********@online.de...
Hi,

I like using RedirectFromLoginPage because it redirects back to the
originally requested page after successful login. However, if the
originally requested page was the login form then it automatically redirects to default.aspx.

How can I preserve this redirection behaviour but specify a different page
to redirect to (other than default.aspx)?

Thanks in advance.

Nov 21 '05 #2
Rufus,

Here is the core logic of my login.aspx page. It does what you want.

If you goto the login.aspx page while already logged in, it will log you
out. (This is a nice feature, so that you can have a "sign out" link on
every page)

If you go directly to the login.aspx page (before being logged in), it
redirects you to page of choice after logging in. (mine happens to be
"hours.aspx")

If you do login succesfully, then attempt to go to page your are not
autherized to go to, it will redirect you back to the login page and explain
why. (cool)

If you attempt to go to a page before logging in it will (of course)
redirect to login page, with an explanation of what happened. (normally I
have the error message commented out, since users already undestand they
need to login before using site)
Hope this isn't information overload, but there is a lot of useful stuff
going on here that I wanted to share. :^)

PS: My login.aspx page mimics hotmail's login; it has a "don't remember
username on public computer" feature. (It did have a remember password
checkbox also, but that is commented out here.)

If anything is not clear, just ask!
Greg
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Dim returnUrl As String = Request.QueryString("ReturnUrl")
If Not (returnUrl = Nothing) Then

' Is the user currently authenticated??
If User.Identity.IsAuthenticated Then
' If YES, then they must be here because they attempted
to access a page
' that they do not have authorization to access

' throw them back to default, not where they were trying
to go
' do this in case cannot login with correct role...
ViewState("RedirectHome") = True
lblReason.Text = "You have arrived at the login page for
the following reason:<br><br>"
lblReason.Text &= "You are not in the correct role to
view the page that your were attempting to view.<br><br>"
Else
' If NO, then they must be here because they attempted
to access a page
' and they have not yet been authenticated
lblReason.Text = "You have arrived at the login page for
the following reason:<br><br>"
lblReason.Text &= "You are not currently
authenticated.<br><br>"
End If
Else
' returnURL is blank, user must have purposefully went to
login page

' if currently sign in, sign em out...
If User.Identity.IsAuthenticated Then
' Redirect to requested URL, or homepage if no previous
page requested
FormsAuthentication.SignOut()
' not running Session.Abandon(), Session.Clear() will
run if sucessful sign in occurs
' don't see need to do it...
'Session.Abandon()
'Response.Redirect("login.aspx")
End If
End If

' prefill username from previous login...
If Not Request.Cookies("RememberMe") Is Nothing AndAlso
Request.Cookies("RememberMe").Value = "1" Then
chkRememberMe.Checked = True
Request.Cookies.Remove("Username")
End If

If Not Request.Cookies("Username") Is Nothing Then
Dim sCookieValue As String =
Request.Cookies("Username").Value
txtUsername.Text = sCookieValue
SetFocus(txtPassword.ClientID)
Else
SetFocus(txtUsername.ClientID)
End If

End If
End Sub

Private Sub LoginClick()

If Not Page.IsValid Then Exit Sub

' Attempt to Validate User Credentials...
Dim EmpID As Integer = eTime.Security.Login(txtUsername.Text,
txtPassword.Text)

If EmpID > 0 Then

' don't do a Session.Abandon, that would get GetEmployeeDetails
to run twice
' once below and again in Session_Start
Session.Clear() ' clear any previous logins!!!

' Lookup the employee's full account details
Dim myEmpDetails As eTime.EmployeeDetails =
eTime.EmployeesDB.GetEmployeeDetails(EmpID)
Session("MyDetails") = myEmpDetails ' save it away!!!

If myEmpDetails.Disabled = True Then

ignoreconditionvalidator1.ErrorMessage = "This account has
been disabled."
ignoreconditionvalidator1.IsValid = False
Exit Sub

ElseIf myEmpDetails.ForcePWDChange = True Then
pnlLogin.Visible = False
pnlPWDChange.Visible = True

SetFocus(txtNew.ClientID)

' save EmpID in viewstate in case sits on change pwd page
longer than session timeout...
ViewState("EmpID") = EmpID
ViewState("PasswordHashWithoutSalt") =
eTime.Security.CreatePasswordHashWithoutSalt(txtPa ssword.Text)
Exit Sub
End If

Authenticate(EmpID, myEmpDetails.Roles)

Else

ignoreconditionvalidator1.ErrorMessage = "Invalid username or
password. Please try again."
ignoreconditionvalidator1.IsValid = False
SetFocus(txtPassword.ClientID)
End If
End Sub

Private Sub Authenticate(ByVal EmpID As Integer, ByVal Roles As String)
' Create a new ticket used for authentication
' Make the cookie persistent only if the user selects "persistent"
login checkbox

Dim ticket As FormsAuthenticationTicket = New
FormsAuthenticationTicket(1, _
EmpID.ToString, _
DateTime.Now, _
DateTime.Now.AddHours(12), _
False, _
Roles)

Dim cookie As HttpCookie = New
HttpCookie(FormsAuthentication.FormsCookieName)
cookie.Value = FormsAuthentication.Encrypt(ticket)

'If (chkRememberLogin.Checked) Then cookie.Expires =
ticket.Expiration 'not currently using this

Response.Cookies.Add(cookie)

If Not chkRememberMe.Checked Then
Dim cook1 As New HttpCookie("Username")
cook1.Expires = DateTime.MaxValue
cook1.Value = txtUsername.Text.ToLower
Response.Cookies.Add(cook1)
Else
Response.Cookies.Remove("Username")
End If

Dim cook2 As New HttpCookie("RememberMe")
cook2.Expires = DateTime.MaxValue 'now.AddDays(1)
If chkRememberMe.Checked Then
cook2.Value = "1"
Else
cook2.Value = "0"
End If
Response.Cookies.Add(cook2)

Dim returnUrl As String
' Redirect to requested URL, or homepage if no previous page
requested (returnURL = nothing when clicked on logout)
returnUrl = Request.QueryString("ReturnUrl")
If (returnUrl = Nothing) Or Not ViewState("RedirectHome") Is Nothing
Then returnUrl = "~\hours.aspx"
' Don't call FormsAuthentication.RedirectFromLoginPage since it
could
' replace the authentication ticket (cookie) we just added
Response.Redirect(returnUrl, False)
End Sub

Private Sub SetFocus(ByVal clientID As String)
Dim strjscript As String = "<script language=""javascript"">"
strjscript &= "document.getElementById(""" & clientID &
""").focus();"
strjscript &= "</script" & ">" 'Don't Ask, Tool Bug
Page.RegisterStartupScript("MYsetfocus", strjscript)
End Sub

"rufus" <he***@online.de> wrote in message news:ci**********@online.de...
Hi,

I like using RedirectFromLoginPage because it redirects back to the
originally requested page after successful login. However, if the
originally requested page was the login form then it automatically
redirects
to default.aspx.

How can I preserve this redirection behaviour but specify a different page
to redirect to (other than default.aspx)?

Thanks in advance.

Nov 21 '05 #3
Much appreciated Greg. A good chance for me to get my hands dirty :)

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Rufus,

Here is the core logic of my login.aspx page. It does what you want.

If you goto the login.aspx page while already logged in, it will log you
out. (This is a nice feature, so that you can have a "sign out" link on
every page)

If you go directly to the login.aspx page (before being logged in), it
redirects you to page of choice after logging in. (mine happens to be
"hours.aspx")

If you do login succesfully, then attempt to go to page your are not
autherized to go to, it will redirect you back to the login page and explain why. (cool)

If you attempt to go to a page before logging in it will (of course)
redirect to login page, with an explanation of what happened. (normally I
have the error message commented out, since users already undestand they
need to login before using site)
Hope this isn't information overload, but there is a lot of useful stuff
going on here that I wanted to share. :^)

PS: My login.aspx page mimics hotmail's login; it has a "don't remember
username on public computer" feature. (It did have a remember password
checkbox also, but that is commented out here.)

If anything is not clear, just ask!
Greg
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Dim returnUrl As String = Request.QueryString("ReturnUrl")
If Not (returnUrl = Nothing) Then

' Is the user currently authenticated??
If User.Identity.IsAuthenticated Then
' If YES, then they must be here because they attempted to access a page
' that they do not have authorization to access

' throw them back to default, not where they were trying to go
' do this in case cannot login with correct role...
ViewState("RedirectHome") = True
lblReason.Text = "You have arrived at the login page for the following reason:<br><br>"
lblReason.Text &= "You are not in the correct role to
view the page that your were attempting to view.<br><br>"
Else
' If NO, then they must be here because they attempted
to access a page
' and they have not yet been authenticated
lblReason.Text = "You have arrived at the login page for the following reason:<br><br>"
lblReason.Text &= "You are not currently
authenticated.<br><br>"
End If
Else
' returnURL is blank, user must have purposefully went to
login page

' if currently sign in, sign em out...
If User.Identity.IsAuthenticated Then
' Redirect to requested URL, or homepage if no previous page requested
FormsAuthentication.SignOut()
' not running Session.Abandon(), Session.Clear() will
run if sucessful sign in occurs
' don't see need to do it...
'Session.Abandon()
'Response.Redirect("login.aspx")
End If
End If

' prefill username from previous login...
If Not Request.Cookies("RememberMe") Is Nothing AndAlso
Request.Cookies("RememberMe").Value = "1" Then
chkRememberMe.Checked = True
Request.Cookies.Remove("Username")
End If

If Not Request.Cookies("Username") Is Nothing Then
Dim sCookieValue As String =
Request.Cookies("Username").Value
txtUsername.Text = sCookieValue
SetFocus(txtPassword.ClientID)
Else
SetFocus(txtUsername.ClientID)
End If

End If
End Sub

Private Sub LoginClick()

If Not Page.IsValid Then Exit Sub

' Attempt to Validate User Credentials...
Dim EmpID As Integer = eTime.Security.Login(txtUsername.Text,
txtPassword.Text)

If EmpID > 0 Then

' don't do a Session.Abandon, that would get GetEmployeeDetails to run twice
' once below and again in Session_Start
Session.Clear() ' clear any previous logins!!!

' Lookup the employee's full account details
Dim myEmpDetails As eTime.EmployeeDetails =
eTime.EmployeesDB.GetEmployeeDetails(EmpID)
Session("MyDetails") = myEmpDetails ' save it away!!!

If myEmpDetails.Disabled = True Then

ignoreconditionvalidator1.ErrorMessage = "This account has
been disabled."
ignoreconditionvalidator1.IsValid = False
Exit Sub

ElseIf myEmpDetails.ForcePWDChange = True Then
pnlLogin.Visible = False
pnlPWDChange.Visible = True

SetFocus(txtNew.ClientID)

' save EmpID in viewstate in case sits on change pwd page
longer than session timeout...
ViewState("EmpID") = EmpID
ViewState("PasswordHashWithoutSalt") =
eTime.Security.CreatePasswordHashWithoutSalt(txtPa ssword.Text)
Exit Sub
End If

Authenticate(EmpID, myEmpDetails.Roles)

Else

ignoreconditionvalidator1.ErrorMessage = "Invalid username or
password. Please try again."
ignoreconditionvalidator1.IsValid = False
SetFocus(txtPassword.ClientID)
End If
End Sub

Private Sub Authenticate(ByVal EmpID As Integer, ByVal Roles As String) ' Create a new ticket used for authentication
' Make the cookie persistent only if the user selects "persistent"
login checkbox

Dim ticket As FormsAuthenticationTicket = New
FormsAuthenticationTicket(1, _
EmpID.ToString, _
DateTime.Now, _
DateTime.Now.AddHours(12), _
False, _
Roles)

Dim cookie As HttpCookie = New
HttpCookie(FormsAuthentication.FormsCookieName)
cookie.Value = FormsAuthentication.Encrypt(ticket)

'If (chkRememberLogin.Checked) Then cookie.Expires =
ticket.Expiration 'not currently using this

Response.Cookies.Add(cookie)

If Not chkRememberMe.Checked Then
Dim cook1 As New HttpCookie("Username")
cook1.Expires = DateTime.MaxValue
cook1.Value = txtUsername.Text.ToLower
Response.Cookies.Add(cook1)
Else
Response.Cookies.Remove("Username")
End If

Dim cook2 As New HttpCookie("RememberMe")
cook2.Expires = DateTime.MaxValue 'now.AddDays(1)
If chkRememberMe.Checked Then
cook2.Value = "1"
Else
cook2.Value = "0"
End If
Response.Cookies.Add(cook2)

Dim returnUrl As String
' Redirect to requested URL, or homepage if no previous page
requested (returnURL = nothing when clicked on logout)
returnUrl = Request.QueryString("ReturnUrl")
If (returnUrl = Nothing) Or Not ViewState("RedirectHome") Is Nothing Then returnUrl = "~\hours.aspx"
' Don't call FormsAuthentication.RedirectFromLoginPage since it
could
' replace the authentication ticket (cookie) we just added
Response.Redirect(returnUrl, False)
End Sub

Private Sub SetFocus(ByVal clientID As String)
Dim strjscript As String = "<script language=""javascript"">"
strjscript &= "document.getElementById(""" & clientID &
""").focus();"
strjscript &= "</script" & ">" 'Don't Ask, Tool Bug
Page.RegisterStartupScript("MYsetfocus", strjscript)
End Sub

"rufus" <he***@online.de> wrote in message news:ci**********@online.de...
Hi,

I like using RedirectFromLoginPage because it redirects back to the
originally requested page after successful login. However, if the
originally requested page was the login form then it automatically
redirects
to default.aspx.

How can I preserve this redirection behaviour but specify a different page to redirect to (other than default.aspx)?

Thanks in advance.


Nov 21 '05 #4

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

Similar topics

0
by: Rob O'Cop | last post by:
Hi, I've got an Intranet site that's been using the usual Forms authentication. Now, I want to retrieve the Windows Login and authenticate the user automatically without her typing...
0
by: Andres Romero | last post by:
I have a login web form named login.aspx, wich validates user from a database. I configure the Web.config file to it authenticates using Form mode, and when the user try acces other page than...
2
by: Gan | last post by:
Hi, I just wonder after calling the RedirectFromLoginPage function, my current page still stick to login.aspx fiile. Any clue?? Thanks
1
by: Murphy | last post by:
I am trying to code a workaround to avoid being redirected to default.aspx if the ReturnURL parameter is blank when the RedirectFromLoginPage method is called. Depending upon the login status the...
2
by: Ben Fidge | last post by:
Is FormsAuthentication.RedirectFromLoginPage the only way to populate the HttpContext.Current.User.Identity.Name property? A base class for each page contains the follwoing property: public...
2
by: Rob O'Cop | last post by:
Hi, I've got an Intranet site that's been using the usual Forms authentication. Now, I want to retrieve the Windows Login and authenticate the user automatically without her typing...
2
by: tshad | last post by:
I have a logon page that is may be called by the Forms Authentication setup. This would put a ReturnURL as part of the URL. I would normally then just issue a: ...
8
by: Andy Sutorius | last post by:
Hi, For some reason the login.aspx webpage redirects to itself after a successful login and not to the url in the address bar. I have stepped through this with debug and it behaves as it is...
8
by: Edward Mitchell | last post by:
I have a main project that is protected in that the user is directed to a login.aspx file. The text in the web.config file is: <authentication mode="Forms"> <forms loginUrl="Login.aspx" />...
4
by: jjjooooohhnnn | last post by:
Greetings, I have encountered what appears to be a fairly common problem: RedirectFromLoginPage is not redirecting to RedirectUrl. I have tried all the advice that I could on the 'net, and...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.