ASP.NET Core HttpContext.User 始终为空

Nik*_*rth 2 .net-core asp.net-core

我使用 asp.net core 3.1 有奇怪的行为,如下所示当我打电话时

在此输入图像描述 这是在 AccountController.Login 操作方法中调用的结果返回成功,用户将重定向到 Dashboard/Index 操作方法,但是当我检查 HttpContext.User.Identity 时 在此输入图像描述

它始终返回空标识对象,该对象的所有内部属性都有默认值。

我尝试手动设置该值,但即使我将其设置为 null,它仍然是空对象

这是我使用的完整的ConfigureService方法 在此输入图像描述

Ren*_*ena 6

您需要确保按照如下顺序调用中间件:UseRoutingUseAuthenticationUseAuthorization、 和UseEndpoints

参考:

https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-3.1&tabs=visual-studio#configure-identity-services