简体   繁体   中英

URL Rewrite IIS, Redirect to login page

I have a webpage that could be accessed by going to

myapp.mydomain.com/Account/Login

I have my project app in the root folder for the site, but If I edit the routing configuration within the app itself this will break things.

I am trying to use URL Rewrite Module so that when a user requests

myapp.mydomian.com

it will redirect him automatically to the full URL above.

I can't figure out how to configure it this way!!

Based on your answer in the comments under the question:

"they go to myapp.mydomian.com to login, thats the first page that they should see"

The correct way to force users to always have to logon to use your application is to use the System.Web.Mvc.Authorize attribute.

Don't try rewriting rules or mess about with routes or drive this from your web.config file <authorize> configuration element.

Taking the template MVC Internet Application as an example, we have two controllers:

  • AccountController
  • HomeController

If we wish to secure just some parts of your MVC application then you'd decorate each controller class with the [Authorize] attribute. For example:

[Authorize]
public class HomeController : Controller
{
    ....
}

However that gets fairly tedious if you wish to secure every controller. Instead you can do this globally by adding a line to your RegisterGlobalFilters static method which is found in the FilterConfig class. This class is located in your project's App_Start folder ( FilterConfig.cs ).

Open this file and make sure you add the following line to the RegisterGlobalFilters static method:

filters.Add(new AuthorizeAttribute());

For example:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new AuthorizeAttribute());  // <-- Add me
}

Also make sure that in your web.config file under your system.web section you have:

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

Out of the box it should be configured like this anyway.

For more information have a read of:

http://www.davidhayden.me/blog/asp.net-mvc-4-allowanonymous-attribute-and-authorize-attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM