且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 ASP.NET MVC 6 重定向未经授权的用户

更新时间:2023-12-01 15:26:28

MVC5(及更早版本):

您可以通过更改 web.config 上的 loginUrl 属性来做到这一点.将其更改为所需的路线:

MVC5 (and older):

You can do this by changing the loginUrl attribute on your web.config. Change it to the desired route:

<authentication mode="Forms">
  <forms loginUrl="~/Home/Index" timeout="2880" />
</authentication>

MVC6:

在 MVC6 中你可以试试这个(在 Startup.cs 中):

public void ConfigureServices(IServiceCollection services)
{       
    services.Configure<CookieAuthenticationOptions>(options =>
    {
        options.LoginPath = new PathString("/Home/Index");
    });
}