因为是后台系统 所以必须登录才可以操作 在之前的ASP.NET MVC下 只需要在web.config中添加下面的代码就可以将所有的未登录请求重定向到login
<authentication mode="Forms">
<forms name="CupidCome" defaultUrl="~/" loginUrl="~/login" protection="All" path="/" />
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
在ASP.NET Core MVC中如何将所有的未登录请求重定向到/Account/Login呢?
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "MyCookieMiddlewareInstance",
LoginPath = new PathString("/Account/Login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
使用 AuthorizeFilter + AuthorizationPolicy :
services.AddMvc(options => { var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); options.Filters.Add(new AuthorizeFilter(policy)); });