.net Identity SignInManager和HttpContext.User.Identity的关系。

问题描述 投票:0回答:1

在我调用的一个控制器方法中。

SignInResult result = await _signInManager.PasswordSignInAsync(username, password, false, false);

它成功地执行了登录,但就在之后(在同一个方法执行中),如果我检查一下 HttpContext.User.Identity 这个对象是空的。

如果我调用另一个api方法,并检查了 HttpContext.User.Identity 这次正确地填写了已登录的用户信息。

为什么只有在后续的调用中才能看到,我如何在登录之后(在同一个方法中)获得身份信息?_signInManager.PasswordSignInAsync

asp.net-core authentication asp.net-identity claims-based-identity
1个回答
0
投票

User 定位于 认证中间件,它在控制器之前执行(见 中间件订单).

因为你可以检查登录是否成功,所以只要自己获取用户就可以了,例如使用 用户管理器:

if (result.Succeeded)
{
    var user = await _userManager.FindByNameAsync(username);
}
© www.soinside.com 2019 - 2024. All rights reserved.