troubleshooting Question

Forms Authentication does not Redirect

Avatar of Robert Treadwell
Robert TreadwellFlag for United States of America asked on
ASP.NETVisual Basic.NET
8 Comments2 Solutions714 ViewsLast Modified:
I have a login page where upon submit the user is returned to the login page per the defaultUrl setting in form tag in web.config file.  In my code I direct my page to redirect base on authenticated users role or authentication.  The onLoggedInEvent subroutine runs correctly however the page still redirects to the defaultUrl.  

I've read many of the formsAuthentication nuances but in the suggested solution of formsAuthenticate.GetRedirectUrl or RedirectFromLoginPage.  these will not work for me because I need to determine redirection path after authentication.

Is there any way to get this to work?  This all worked on windows 3.5 server now moving to windows 4.0.

web.config parameters:

      <authentication mode="Forms">
          <forms defaultUrl="Login.aspx" protection="All" name=".ASPXFORMSAUTH" loginUrl="Login.aspx" timeout="15" requireSSL="true" domain=""/>
      <httpCookies requireSSL="true"/>
      <sessionState timeout="1"/>

Protected Sub onAuthenticatedEvent(ByVal sender As Object, ByVal e As AuthenticateEventArgs)
        Dim userNameTB As TextBox = CType(LoginView1.FindControl("ADCLogin").FindControl("UserName"), TextBox)
        Dim passwordTB As TextBox = CType(LoginView1.FindControl("ADCLogin").FindControl("Password"), TextBox)

            If Membership.ValidateUser(userNameTB.Text, passwordTB.Text) Then
                e.Authenticated = True
                FormsAuthentication.SetAuthCookie(userNameTB.Text, False)
                Session("authUserName") = userNameTB.Text
            End If

        Catch ex As Exception
            Response.Write("onAuthenticatedEvent produce the following error: " + ex.Message)
        End Try
    End Sub

    Protected Sub onLoggedInEvent(ByVal sender As Object, ByVal e As EventArgs)
        Dim userNameTB As TextBox = CType(LoginView1.FindControl("ADCLogin").FindControl("UserName"), TextBox)

            'Register Action buttons that are stored and selected in the forms Master Page
            'This code works in conjunction with the Event handler in the ListViewMasterPage VB code
            Dim master As MasterPages_FormsMasterPage = TryCast(Me.Master, MasterPages_FormsMasterPage)

            'Now Direct user based on Role
            If Roles.IsUserInRole(userNameTB.Text, "Administrator") Or Roles.IsUserInRole(userNameTB.Text, "Site Admin") Then
                redirectUrl = "~/Lists_Views/OrgEditView.aspx"
            ElseIf Roles.IsUserInRole(userNameTB.Text, "Assistant") Then
                redirectUrl = "~/Lists_Views/StaffEditView.aspx"
            ElseIf Roles.IsUserInRole(userNameTB.Text, "Temporary User") Then
                redirectUrl = "~/Forms/OrganizationKeeper.aspx"
                redirectUrl = "~/Lists_Views/ClientByName.aspx"
            End If

            Response.Redirect(redirectUrl, False)
        Catch ex As Exception
            Response.Write("OnLoggedInEvent produce the following error: " + ex.Message)
        End Try
    End Sub
Robert Treadwell
Managing Director

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros