In an ASP.NET MVC3 web application, an entire controller has an [Authorize]
attribute attached to it. So if the user is not logged in or the session expired, they get redirected to the login page. This is working...sometimes. The URLs in the "works" list below correctly redirect to the login page; the URLs in the "does not work" list instead show the IIS 401 error screen - they do not redirect to the login page.
Works
- http://x.y.z/MyController/MyAction
- http://x.y.z/MyController/MyAction/123
- http://x.y.z/MyController/MyAction/123?X=Y
Does Not Work
- http://x.y.z/MyController/MyAction/123?ReturnUrl=
- http://x.y.z/MyController/MyAction/123?ReturnUrl=XYZ
The model for the MyAction
action has a public string ReturnUrl { get; set; }
in its base class. It also has other properties, but adding those to the query string does not affect the login redirection. It seems to be only the ReturnUrl parameter.
I'm not sure what else to look into. Any ideas why the ReturnUrl
parameters would be causing trouble?
Routes
routes.MapRoute("Default-Title-ID", "{Controller}/{Action}/{Title}_{ID}", namespaces);
routes.MapRoute("Default-ID", "{Controller}/{Action}/{ID}", namespaces);
routes.MapRoute("Default", "{Controller}/{Action}", new { Controller = "Home", Action = "Index" }, namespaces);
routes.MapPageRoute("Reports-View", "ViewReport_{ID}", "~/Views/Reports/View.aspx");
Working Example (Well, not working, but illustrates the problem.)
Download the solution here: https://docs.google.com/file/d/0B4o6vqgNLpvbeVo4bVdKZWFMcEE/edit?usp=sharing
And then try to visit:
- http://your.local.host/Test/TestMe?ReturnUrl= - you will not be redirected to the login page.
- http://your.local.host/Test/TestMe - you will be redirected to the login page.
Default-Title-ID
andDefault-Title-ID
-- They both have a 3rd ID param, but the route engine would NOT know which to choose when using url's. TrueDefault
is the complement, in that both Controller and Action are optional, but that route is traditionally implemented as part of the 1st of 2nd to give the route engine more flexibility. I would strongly suggest trying to merge the 3 routes, then seeing if you still have the same problem. – Dave A Apr 20 at 5:34Default-Title-ID
andDefault
routes. – Josh M. Apr 23 at 11:23