Startup.cs:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "CustomAuthenticationCookieMiddleware",
LoginPath = new PathString("/user/login"),
LogoutPath = new PathString("/user/logout"),
AccessDeniedPath = new PathString("/access-denied"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
MembershipController.cs
...
await HttpContext.Authentication.SignInAsync("CustomAuthenticationCookieMiddleware", claimsPrincipal, new AuthenticationProperties { IsPersistent = loginUser.RememberMe });
...
======
问题:
[Authorize]
属性不起作用。它会重定向到访问被拒绝的页面。
但[Authorize(Roles = "Administrator")]
效果很好
注意:即使我成功登录<{p>,"User.Identity.IsAuthenticated"
也始终为假
答案 0 :(得分:1)
我引用:
同样,对于Forbidden响应,用户被重定向到 我们将中间件添加到我们的AccessDeniedPath中指定的路径 管道。在这种情况下,我们不会重定向到登录路径 用户已经过身份验证,他们只是没有正确的声明 或查看所请求资源的权限
阅读这篇优秀文章并解决您的问题:https://andrewlock.net/exploring-the-cookieauthenticationmiddleware-in-asp-net-core/