即使记录,AuthorizeAttribute也会重定向到访问被拒绝的页面

时间:2016-08-16 17:50:45

标签: asp.net asp.net-core asp.net-identity asp.net-core-mvc asp.net-core-middleware

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"也始终为假

1 个答案:

答案 0 :(得分:1)

我引用:

  

同样,对于Forbidden响应,用户被重定向到   我们将中间件添加到我们的AccessDeniedPath中指定的路径   管道。在这种情况下,我们不会重定向到登录路径   用户已经过身份验证,他们只是没有正确的声明   或查看所请求资源的权限

阅读这篇优秀文章并解决您的问题:https://andrewlock.net/exploring-the-cookieauthenticationmiddleware-in-asp-net-core/