Search code examples
.netasp.net-mvcauthenticationiis-7forms-authentication

Forms Authentication & IIS7 (&MVC): Why ReturnUrl=/ is added?


When trying to access my site:

www.X.com

The browser changes the url to:

www.X.com/

The problem is that the result url is:

www.X.com/HomePage.aspx?ReturnUrl=/

(HomePage.aspx is the default page)

On IE: www.X.com/HomePage.aspx?ReturnUrl=%2f

For some reason the Forms Authentication treats / as a page that the user is trying to access and then gets redirects to: HomePage.aspx?ReturnUrl=/

How can I set the Forms Authentication (or the MVC routing) not to treat / as a page, so when accessing www.X.com it will not change the url?

The site runs on windows server 2008 IIS7, .NET 4.

(When running on IIS6 it didn't have this problem)

Web.config:

<authentication mode="Forms">
            <forms name=".AUTHCOOKIE" loginUrl="HomePage.aspx" defaultUrl="Loading.aspx" timeout="9480" />
  </authentication>

MVC Routing (not sure it’s related):

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
        routes.IgnoreRoute("{resource}.ascx/{*pathInfo}");
        routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
        routes.IgnoreRoute("{resource}.gif/{*pathInfo}");

        routes.MapRoute("Actions", "A/{controller}/{action}", new { controller = "Tasks", action = "InitPage" });

    }

Thanks

Rafael


Solution

  • Actually, when you access http://www.x.com, you are requesting http://www.x.com/. When you get redirected to the validation form in IIS/.Net, RedirectTo= is always appended at the end of the URL, so that the server knows where do you wanted to go before your request was intercepted.

    Or to put it another way: everything is working as expected. What's exactly what you want to do?

    If it helps, I see two things "weird" with the code you included:

    • The Login page is called HomePage.aspx. That's... unusual (with that name, HomePage.aspx should be the page you go to after login, not the login page). Moreover, you mention MVC, but that's not a MVC route.
    • Your only route in Global.asax starts with "A/", so that http://www.x.com/ won't be captured by it. If your start page must be http://www.x.com/A/Tasks/InitPage, either make A a route parameter {whatever}, and assign it a default value, or create another MapRoute that captures the "/" request, and redirects to the correct path. Probably adding a Default.aspx to the project would work, but it would be very un-MVC.