Solved

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

Posted on 2011-03-08
5
801 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

11 Experts available now in Live!

Get 1:1 Help Now