ASP.NET Core 能够通过配置文件中的 Authentication 部分或 Startup.cs 文件中的 ConfigureServices 方法来指定登录页的路径。如果未显式指定,则系统默认将路径设置为 "/Account/Login"。
在 Startup.cs 中,可以使用下面的代码示例来指定登录页的路径:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.LoginPath = "/CustomLoginPath";
});
上面的代码将登录页路径设置为 "/CustomLoginPath"。如果需要修改其他配置项,则可以在 options 对象中进行设置。
在配置文件中,可以打开 appsettings.json(或 appsettings.Development.json)文件并找到 Authentication 部分,如下所示:
{
"Authentication": {
"CookieAuthenticationOptions": {
"LoginPath": "/CustomLoginPath"
}
}
}
同样地,上面的配置会将登录页路径设置为 "/CustomLoginPath"。如果需要指定其他选项,则可以在此处指定。
在任何情况下,都可以通过使用 UseAuthentication() 方法来启用身份验证。在 ASP.NET Core 中,通常将身份验证中间件放置在管道的最前面,以使所有请求都受到保护。