简体   繁体   English

URL重写IIS,重定向到登录页面

[英]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应用程序为例,我们有两个控制器:

  • AccountController 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. 如果我们只希望保护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.

相关问题 IIS 上托管应用程序的 URL 重写 - Url Rewrite for hosted application on IIS IIS 7.5 URL重写:从旧域到新域的重定向规则似乎不起作用 - IIS 7.5 URL Rewrite: Redirect Rule does not appear to work from old domain to new domain 如果用户输入直接URL,则限制对所有视图的直接访问,显示/重定向到错误页面(登录页面除外) - Restrict direct access to All views show/Redirect to error page except Login page, if user enters direct URL 为什么我的 IIS 301 永久重定向重写持久/不恢复? - Why is my IIS 301 permanent redirect rewrite persistent / not reverting? IIS 7.5下的ASP Net Web应用程序重定向到登录页面,但登录后出现错误http 404.0 - asp net web application under IIS 7.5 redirect to a login page but gives error http 404.0 after log-in IIS 8.5 URL将非www重写为www不起作用 - IIS 8.5 URL Rewrite non-www to www not working 重定向URL并在该页面上运行脚本 - Redirect URL and run script on that page MVC 4如果经过身份验证,则从登录页面重定向 - MVC 4 Redirect from login page if authenticated 一段时间后自动重定向到登录页面 - Automatic redirect to login page after a certain time 在MVC4中提交后重定向到登录页面 - Redirect To Login Page After Submit In MVC4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM