【问题标题】:AuthorizeAttribute redirects to access denied page even if loggedAuthorizeAttribute 重定向到访问被拒绝的页面,即使已记录
【发布时间】:2016-12-23 05:04:16
【问题描述】:

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

【问题讨论】:

  • 您在Configure 方法中是否在UseCookieAuthentication 之前使用UseIdentity() 或其他身份验证中间件?
  • 不要只使用上面的代码
  • 您是使用 ASP.NET 默认身份验证机制还是使用自定义身份验证?您的项目中有任何自定义 Authorize 属性吗?
  • 自定义身份验证。但我不使用任何自定义授权属性
  • 我认为发布的代码效果很好。你能发布 Startup.cs 和 Controller 代码吗?

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


【解决方案1】:

我在引用:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2012-03-03
    • 2016-12-22
    • 1970-01-01
    相关资源
    最近更新 更多