[英]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 我正在尝试使用URL重写模块,以便在用户请求时
myapp.mydomian.com
it will redirect him automatically to the full URL above. 它将自动将他重定向到上面的完整URL。
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"
“他们去myapp.mydomian.com进行登录,那就是他们应该看到的第一页”
The correct way to force users to always have to logon to use your application is to use the System.Web.Mvc.Authorize
attribute. 强制用户始终必须登录才能使用您的应用程序的正确方法是使用
System.Web.Mvc.Authorize
属性。
Don't try rewriting rules or mess about with routes or drive this from your web.config
file <authorize>
configuration element. 不要尝试重写规则或弄乱路由,也不要通过
web.config
文件<authorize>
配置元素来驱动它。
Taking the template MVC Internet Application as an example, we have two controllers: 以模板MVC Internet应用程序为例,我们有两个控制器:
If we wish to secure just some parts of your MVC application then you'd decorate each controller class with the [Authorize]
attribute. 如果我们只希望保护MVC应用程序的某些部分,则可以使用
[Authorize]
属性装饰每个控制器类。 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. 相反,您可以通过在
FilterConfig
类中找到的RegisterGlobalFilters
静态方法中添加一行来全局地执行此操作。 This class is located in your project's App_Start
folder ( FilterConfig.cs
). 此类位于项目的
App_Start
文件夹( FilterConfig.cs
)中。
Open this file and make sure you add the following line to the RegisterGlobalFilters
static method: 打开此文件,并确保将以下行添加到
RegisterGlobalFilters
静态方法:
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: 还要确保在
system.web
部分下的web.config
文件中,您具有:
<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
http://www.davidhayden.me/blog/asp.net-mvc-4-allowanonymous-attribute-and-authorize-attribute
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.