Problem redirecting a page from httpmodule

Last post 05-15-2008 11:51 AM by DigiMortal. 6 replies.

Sort Posts:

  • Problem redirecting a page from httpmodule

    05-10-2008, 7:55 AM
    • Loading...
    • sreedath
    • Joined on 07-11-2006, 6:25 AM
    • Posts 9

    Hi,

    There are two pages one for the Admin and other for the normal user.Depending on the user they need to be redirected to a different page.I am writing the logic for the redirection in the httpmodule beginrequest handler.Both the pages share a common suffix(dashboard).But on being redirected I am entering into an infinite loop.Is there a way to stop the iteration once the redirection is done the first time??I am attaching a sample code 

    void context_BeginRequest(object sender, EventArgs e)

    {

    //method1 populates the cookie with values from a DB call

    //the code below is a sample

    if(HttpContext.Current.Request.RawUrl.Contains("Dashboard.aspx"))

    if(user=="Admin")

    {

     HttpContext.Current.RewritePath("AdminDashboard.aspx");

    }

    else

    {

    HttpContext.Current.RewritePath("UserDashboard.aspx");

    }

    }

    }

     

    Thank you in advance!

  • Re: Problem redirecting a page from httpmodule

    05-10-2008, 8:36 AM
    • Loading...
    • DigiMortal
    • Joined on 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 304

    Yes, you must be in infinite loop. You are checking if url contains Dashboard.aspx. Both - AdminDashboard.aspx and UserDashboard.aspx contain Dashboard.aspx. Instead of

    if(HttpContext.Current.Request.RawUrl.Contains("Dashboard.aspx"))

    you should check out if request is made to page calles Dashboard.aspx. To do this you have to check Reuqest object's path properties.

    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: Problem redirecting a page from httpmodule

    05-11-2008, 10:26 AM
    • Loading...
    • sreedath
    • Joined on 07-11-2006, 6:25 AM
    • Posts 9

    Thank You DigiMortal for the reply.

    But again won't there be a infinite loop?What if the requested path is  ../AdminDashBoard.aspx and the user needs to be redireced to the same page?Sorry if I have misunderstood your answer.Might be sample code will help

     The way I tackled the issue is by redirecting to a common page LandingDashBoard(Non-Existent).And checking if the request is for LandingDashBoard.aspx,then redirect to the specific  dashboard page depending on the role.But this again suffers from the issue that if the user comes to know of the url of admin page then he will be able to browse it.

    Is there a better way or better place to put in this logic??

  • Re: Problem redirecting a page from httpmodule

    05-11-2008, 11:26 AM
    • Loading...
    • DigiMortal
    • Joined on 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 304
    Extract the file name part from URL and check if it is DashBoard.aspx. If it IS (not like your condition: name contains) EQUAL to Deashboard.aspx then decide where to redirect.
    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: Problem redirecting a page from httpmodule

    05-12-2008, 2:00 AM
    • Loading...
    • sreedath
    • Joined on 07-11-2006, 6:25 AM
    • Posts 9

    DigiMortal the problem is there is no DashBoard.aspx.We have only two pages AdminDashBoard.aspx and UserDashboard.aspx.I am only trying to check if the user has requested for one of the DashBoard pages?
    if he is being redirected to the correct page is there a way to stop the flow going back to the begin request handler?

  • Re: Problem redirecting a page from httpmodule

    14 hours, 22 minutes ago
    • Loading...
    • docluv
    • Joined on 06-29-2002, 11:16 PM
    • Willow Spring NC
    • Posts 1,316
    void context_BeginRequest(object sender, EventArgs e)

    {

    //method1 populates the cookie with values from a DB call

    //the code below is a sample

    if(HttpContext.Current.Request.RawUrl.ToLower() == "dashboard.aspx")

    if(user=="Admin")

    {

     HttpContext.Current.RewritePath("AdminDashboard.aspx");

    }

    else

    {

    HttpContext.Current.RewritePath("UserDashboard.aspx");

    }

    }

    }

  • Re: Problem redirecting a page from httpmodule

    11 hours, 9 minutes ago
    • Loading...
    • DigiMortal
    • Joined on 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 304

    This one doesn't work if application is not in site root. And if it is, it asks teh file /dashboard.aspx

    Before trying hacks try to get file name from Request properties. It should be there even if file doesn't exist.

    If you want to extract file name from RawUrl then follow these steps:

    • Split RawUrl by using '/' as delimiter.
    • Take string at last position of array you got.
    • Split it by using '?' as delimiter.
    • Take string at first position of array tou got.
    • Make this string lowercase and see if it is "deashboard.aspx" or not.
    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
Page 1 of 1 (7 items)