[英]ASP.NET Core 2.0 Cookie Authentication without identity not directing to LoginPath
Moved from ASP.NET Core 1.1 to 2.0 and having issues with cookie authentication.从 ASP.NET Core 1.1 移动到 2.0 并且存在 cookie 身份验证问题。
The application will not follow the LoginPath
and goes directly to the AccessDeniedPath
.应用程序不会遵循
LoginPath
并直接转到AccessDeniedPath
。
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/Account/Login/");
options.AccessDeniedPath = new PathString("/Account/Forbidden/");
});
services.AddAuthorization(options =>
{
options.AddPolicy(Constants.CONST_POLICY_SUPERADMIN, policy => policy.RequireRole(Constants.CONST_ROLE_SUPERADMIN));
options.AddPolicy(Constants.CONST_POLICY_ADMIN, policy => policy.RequireRole(Constants.CONST_ROLE_ADMIN, Constants.CONST_ROLE_SUPERADMIN));
options.AddPolicy(Constants.CONST_POLICY_DIR, policy => policy.RequireRole(Constants.CONST_ROLE_ADMIN, Constants.CONST_ROLE_SUPERADMIN, Constants.CONST_ROLE_DIR));
options.AddPolicy(Constants.CONST_POLICY_HoD, policy => policy.RequireRole(Constants.CONST_ROLE_ADMIN, Constants.CONST_ROLE_SUPERADMIN, Constants.CONST_ROLE_DIR, Constants.CONST_ROLE_HoD));
options.AddPolicy(Constants.CONST_POLICY_STAFF, policy => policy.RequireRole(Constants.CONST_ROLE_ADMIN, Constants.CONST_ROLE_SUPERADMIN, Constants.CONST_ROLE_DIR, Constants.CONST_ROLE_HoD, Constants.CONST_ROLE_STAFF));
});
}
This does not redirect to the login method at all.这根本不会重定向到登录方法。 While testing I changed the
AccessDeniedPath
to point at the Login method, and it logs the user in fine.在测试时,我将
AccessDeniedPath
更改为指向 Login 方法,并且它可以很好地记录用户。
Completely stumped as to why the LoginPath
doesn't direct to the Login
method.完全不明白为什么
LoginPath
不指向Login
方法。
Add [Authorize] to the controller(s) you wish to force redirect from. 将[Authorize]添加到您要强制从其重定向的控制器。
eg 例如
using Microsoft.AspNetCore.Authorization;
[Authorize]
public class HomeController : Controller
{ ....
According to the documentation , the first you need in ConfigureServices
is to add Identity. 根据文档 ,您首先需要在
ConfigureServices
中添加Identity。 Something like: 就像是:
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
// Signin settings
options.SignIn.RequireConfirmedEmail = true;
options.SignIn.RequireConfirmedPhoneNumber = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.