Answered by:
The page isn’t redirecting properly

Question
-
User1216627406 posted
Greetings experts,
Hope everyone is doing their best to stay safe.
I have been struggling with this problem now for more than three days and your prompt assistance is greatly appreciated.
When I fire up the browser and load this web app that was built on MasterPages,
http://servername:8080/myFolder/Restricted.aspx
It takes me to Restricted.aspx as the URL above shows but rather than displaying the message on the Restricted page, I get the following server error instead:
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.
Even though I get this message on Firefox, I get similar (not exact message) on IE and Chrome.
I have followed the instructions to clear cookies but the message persists.
Here is my Restricted.aspx and Restricted.aspx.cs page:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Restricted.aspx.cs" Inherits="ToiletRebate2.Restricted" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %> <%@ MasterType VirtualPath="~/Site.Master" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h1>ACCESS RESTRICTED</h1> <h3>This application has a restricted your access to the page you were attempting to get into. If you feel you have received this message in error, please contact the Tech Support team .</h3> <h5>Web Master</h5> </asp:Content> //C# public partial class Restricted : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Master.Restricted(); } }
Could it be as a result of having the virtualPath at top of page?
<%@ MasterType VirtualPath="~/Site.Master" %>
I am really stumped by this error.
Thank you very much in advance for your help.
Sunday, December 6, 2020 6:01 PM
Answers
-
User753101303 posted
I tested with just
if (Request.Url.AbsolutePath.ToLower() != "/myprojectfolder/restricted.aspx") { Response.Redirect("/myprojectfolder/restricted.aspx"); }
In all pages using this master page I'm redirected to restricted.aspx and on restricted.aspx I'm just left on the current page wihtout any error. Do you have SEEN if the problem is really with this page or not.? Don't use a vague description such as "blew up"? Do you mean that you still have the "The page isn’t redirecting properly" message?
Also you used the suggested browser tool to see which redirections you have? Or start with:
if(Request.Url.AbsolutePath.ToLower().EndsWith("restricted.aspx")) return; // This is in the master page but you don't want to run this code when the master page is used for restricted.aspx if (WebLibrary.GroupIsMember("AppUsers")) { NavigationMenu.Items[4].ChildItems.Add(new MenuItem("Error", "", "", "~/Error.aspx")); userType = "Modify"; } else if (WebLibrary.GroupIsMember("OnlyRead")) { NavigationMenu.Items[2].Enabled = false; } else { Response.Redirect("~/Restricted.aspx"); }}
Edit: as pointed already this is not the usual approach but it depends for example if you are using built in roles or some custom code. ASP.NET allows to define required roles for pages or folderss and redirect autlomatically the user to the login page if the rrequest is rejected alllowing to do something very similar with basically no custom code.
Ah and if not done already please use the suggested browser tool to be 100% sure about which redirection loop you have in your app.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 10, 2020 6:34 PM
All replies
-
User-939850651 posted
Hi simflex,
Regarding the problem you described, I did some searching and I am not sure if you have seen this case:
The page isn't redirecting properly
And I am confused by the code you provided:
protected void Page_Load(object sender, EventArgs e) { Master.Restricted(); }
I did not find the use of this function in the Master.
If possible, could you provide more details?
Best regards,
Xudong Peng
Monday, December 7, 2020 8:32 AM -
User1216627406 posted
You are correct XuDong that Master.Restricted() doesn't appear to be doing anything.
The only thing I can say about it is that it is referenced in the code below. Also, in the code below, there is a Response.Redirect to Restricted.aspx and as you can see from the URL I posted, it gets to that page except that for reasons I can't explain, does not display the contents.
Let me note here also that on my local machine, when you run it, it displays the contents of Default.aspx but on the server, it tries to redirect to Restrict.aspx.
It would be nice to know why the user is being redirected to that restricted page in the first place. I did not write this code. I am just asked to try and figure out the issues it is having.
Thanks again for your help sir.
if (WebLibrary.GroupIsMember("AppUsers")) { NavigationMenu.Items[4].ChildItems.Add(new MenuItem("Error", "", "", "~/Error.aspx")); userType = "Modify"; } else if (WebLibrary.GroupIsMember("OnlyRead")) { NavigationMenu.Items[2].Enabled = false; } else { Response.Redirect("~/Restricted.aspx"); }}
Monday, December 7, 2020 7:19 PM -
User-939850651 posted
Hi simflex,
According to the code you provided, I obviously cannot reproduce your problem and perform related tests. Have you debugged this part of the code?
If you use breakpoints to debug the code and view its specific operation process, I believe it will help solve the problem.
Best regards,
Xudong Peng
Tuesday, December 8, 2020 10:36 AM -
User753101303 posted
Hi,
Looks like it could be a "redirection loop". If the user is not in one of those groups he is directed to the "restricted" page which uses the same master page which could cause the browser to be directed again and again to the same "restricted" page until it gives up. You can use F12 Network in your browser to see that.
If confirmed you should likely use Request.ServerVariables["SCRIPT_NAME"] (or through a property in Request.Url) to test if you are not already on this page before doing the redirect (this page really needs the same master page ?)
Tuesday, December 8, 2020 11:11 AM -
User1216627406 posted
PatriceSC, thank you very much for your proposed solution.
It took me awhile to get back here because I have been testing out your suggestions.
I tried this:
If (Request.UrlReferrer != null) { Response.Redirect("Restrict.aspx"):} else { Response.Write("Oops! Something is wrong":}
What this did was bypass Restrict.aspx page and display a page that is supposed to show who logged on to the app.
It did not resolve the issue of loading Restricted page and its contents.
Any ideas how this could have been handled differently with the code above?
Thursday, December 10, 2020 3:25 AM -
User753101303 posted
I was thinking about something such as:
if (Request.Url.AbsolutePath.ToLower() != "/restricted.aspx") { Response.Redirect("/restricted.aspx"): }
ie I believe that currently the master page is directing the user to restricted.aspx even when being already on this page.
FYI at a later time I believe it could be enhanced if needed by taking advantage of built in ASP.NET features such as "url authorization" which allows to configure required roles for a page or folder etc...
Thursday, December 10, 2020 8:40 AM -
User1216627406 posted
Yea, it's back to that stupid error again:
The page isn’t redirecting properly
Thanks a lot for your time and help anyway.
Thursday, December 10, 2020 2:24 PM -
User475983607 posted
The design is very fragile. Every redirect from the master page must check if the current request is NOT the redirect page. Otherwise an infinite loop is generated. This situation becomes exponentially more complex if the content pages have redirects too.
It is difficult to provide a concrete solution without the business requirements. Anyway, this situation happens to everyone at some point. The solution is usually an "if" condition that stops the redirect loop.
Thursday, December 10, 2020 3:38 PM -
User753101303 posted
Then start with https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor to make 100% sure about which bad redriection you have.
Your browser is counting redirects and when exceeding a given count it stops and shows an errror message,.You are still using a restrictec.aspx page at the root of your web form or you changed that since then?
Thursday, December 10, 2020 3:44 PM -
User1216627406 posted
@mgebhard, you are right; the design is very fragile.
I am not one of the best .net developers, not even close but I am very confident I could have done better.
Management doesn't want the entire app tossed and rewritten and there is so much I can handle on my own.
Thursday, December 10, 2020 4:49 PM -
User1216627406 posted
You are still using a restrictec.aspx page at the root of your web form or you changed that since then?
No, I have not changed it.
When I ran your code exactly as is, I got an error that /restrict.aspx could not be found.
The reason for the error was that after running the app, the URL shows http://servername:8080/restrict.aspx
It should show http://servername:8080/projectfolder/restrict.aspx.
So, what I did was change the code slightly:
if (Request.Url.AbsolutePath.ToLower() != "/myprojectfolder/restricted.aspx") { Response.Redirect("/myprojectfolder/restricted.aspx"); }
Then it blew up again.
Do I need to use fully qualified domain like:
Response.Redirect("http://servername:8080/myprojectfolder/restricted.aspx"); ?
Thursday, December 10, 2020 4:56 PM -
User753101303 posted
And so your page is restricted.aspx as in your first post or restrict.aspx as you are showing niow? Of course it needs to match the actual page name you are using.
Thursday, December 10, 2020 5:16 PM -
User1216627406 posted
Sorry, it is restricted. That was a typo on this forum but I have used the correct name = restricted on my app.
Thursday, December 10, 2020 5:27 PM -
User475983607 posted
I am not one of the best .net developers, not even close but I am very confident I could have done better.
Management doesn't want the entire app tossed and rewritten and there is so much I can handle on my own.
There is no reason to toss the application. You just need to understand what's happening and fix the bug. The design is fragile because the redirects are in the Master Page. You must check the current page and make sure you are not redirecting to the same page. You also have to check the content pages and make sure there are no redirects that can cause the same infinite redirect loop.
Thursday, December 10, 2020 6:06 PM -
User753101303 posted
I tested with just
if (Request.Url.AbsolutePath.ToLower() != "/myprojectfolder/restricted.aspx") { Response.Redirect("/myprojectfolder/restricted.aspx"); }
In all pages using this master page I'm redirected to restricted.aspx and on restricted.aspx I'm just left on the current page wihtout any error. Do you have SEEN if the problem is really with this page or not.? Don't use a vague description such as "blew up"? Do you mean that you still have the "The page isn’t redirecting properly" message?
Also you used the suggested browser tool to see which redirections you have? Or start with:
if(Request.Url.AbsolutePath.ToLower().EndsWith("restricted.aspx")) return; // This is in the master page but you don't want to run this code when the master page is used for restricted.aspx if (WebLibrary.GroupIsMember("AppUsers")) { NavigationMenu.Items[4].ChildItems.Add(new MenuItem("Error", "", "", "~/Error.aspx")); userType = "Modify"; } else if (WebLibrary.GroupIsMember("OnlyRead")) { NavigationMenu.Items[2].Enabled = false; } else { Response.Redirect("~/Restricted.aspx"); }}
Edit: as pointed already this is not the usual approach but it depends for example if you are using built in roles or some custom code. ASP.NET allows to define required roles for pages or folderss and redirect autlomatically the user to the login page if the rrequest is rejected alllowing to do something very similar with basically no custom code.
Ah and if not done already please use the suggested browser tool to be 100% sure about which redirection loop you have in your app.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 10, 2020 6:34 PM -
User1216627406 posted
Thank you again for your continued assistance.
Do you have SEEN if the problem is really with this page or not.?
If I understand you correctly, you want to know if this problem I am having is with just this page, restricted.aspx?
So far, that appears to be the only page with this problem.
However, given that user is not getting authenticated, perhaps, user does not belong to any of those groups, any page you click on, gives you that same error message of The page isn’t redirecting properly which is what I meant by blew up, sorry about that. I will be more specific next time.
I am going to test your latest code and I will be back shortly with an update.
Thank you very much again @PatriceSC for your help.
Oh almost forgot, yes, I used the browser tool but not sure what I should be looking out for. That network tab is just showing several instances of http://servername:8080/myprojectpage/restricted.aspx and this is repeated over and over again on that page.
Thursday, December 10, 2020 6:53 PM -
User1216627406 posted
YEA!!!!!
@PatriceSC, you are INVINCIBLE!!!!
It is WORKING PERFECTLY!
Thank you, thank you, thank you.
Thursday, December 10, 2020 7:15 PM