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")]
运行得很好
注意:即使我成功登录,"User.Identity.IsAuthenticated"
也总是错误的。
发布于 2016-08-20 08:42:32
我引用如下:
类似地,对于禁止的响应,当我们将中间件添加到管道时,用户将被重定向到AccessDeniedPath中指定的路径。在本例中,我们不重定向到登录路径,因为用户已经通过身份验证,他们只是没有正确的声明或权限来查看所请求的资源
阅读这篇优秀的文章并解决您的问题:https://andrewlock.net/exploring-the-cookieauthenticationmiddleware-in-asp-net-core/
https://stackoverflow.com/questions/38981382
复制相似问题