【发布时间】: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