Solved

Global.asax Redirect is removing CSS and producing Java script error as "syntax error code 0"

Posted on 2011-03-08
5
802 Views
Last Modified: 2012-05-11
Hi

I have the code below to in the global.asax to redirect users to the change password page when its been 48 since they last cahnged thir passowrd


 
Sub Application_PostAuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        'code that runs after the person has been authicated
        If User.Identity.IsAuthenticated Then
 
              Dim userdetails As MembershipUser = Membership.GetUser
            ' If userdetails.LastPasswordChangedDate < Today.AddDays(-48) Then
            If Request.RawUrl.Contains("/Account/ChangePassword.aspx") = False Then

                Response.Redirect("~/Account/ChangePassword.aspx")
            End If
        'End If
        End If
    End Sub

Open in new window


The problem is that when this redirect is triggered the change password page loads without any css styling and with 2 "syntax errors code 0" in IE.

going to the change passowrd page normally or from redirects from other pages, it all loads fine.

the problem is just when loading via a redirect from the global.asax

Any ideas what to look for.
0
Comment
Question by:bolt81
  • 2
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35068128
I would guess it is an issue with performing a redirect when either WebResource.axd or ScriptResource.axd are triggering the PostAuthenticationRequest handler. Try changing your code to:
Sub Application_PostAuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
    If TypeOf(context.Handler) Is IRequiresSessionState Or TypeOf(context.Handler) Is IReadOnlySessionState Then

        'code that runs after the person has been authicated
        If User.Identity.IsAuthenticated Then
 
              Dim userdetails As MembershipUser = Membership.GetUser
            ' If userdetails.LastPasswordChangedDate < Today.AddDays(-48) Then
            If Request.RawUrl.Contains("/Account/ChangePassword.aspx") = False Then

                Response.Redirect("~/Account/ChangePassword.aspx")
            End If
        'End If
        End If

    End If
End Sub

Open in new window

0
 
LVL 2

Expert Comment

by:rahulbagal
ID: 35068143
I would suggest instead of redirecting the user from global.asax  you can set a flag there & do redirection in page load of other pages .

0
 
LVL 1

Author Comment

by:bolt81
ID: 35068356
I've added the above code and it stopped the redirect happening altogether.

i want to do it in the global so that users are redirected no matter what page they go to.

I could  place a piece of code at the top of each page to handle this, but isn't this the idea of the global.asax?

If anyone has a alternative way to do this i'm all ears

thanks
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35068484
The problem is that that event doesn't just fire for pages, it also fires for other resources which you need to exclude. You could try the more "sledgehammer" approach of:
Sub Application_PostAuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)

        'code that runs after the person has been authicated
        If User.Identity.IsAuthenticated Then
 
              Dim userdetails As MembershipUser = Membership.GetUser
            ' If userdetails.LastPasswordChangedDate < Today.AddDays(-48) Then
            If Request.RawUrl.Contains("/Account/ChangePassword.aspx") = False And Request.RawUrl.Contains(".axd") = False Then

                Response.Redirect("~/Account/ChangePassword.aspx")
            End If
        'End If
        End If

End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:bolt81
ID: 35068659
That fixed it, This stopped the javascript errors.

i added "And Request.RawUrl.Contains(".css") = False", which stopped the issue with the css not running as well
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now