我正在尝试将用户数据作为ClaimsPrincipal
从一个API服务传递给另一服务。
[HttpPost("getexternal")]
public async Task<IActionResult> GetUserByExternalLogin(ExternalSignModel model, [FromServices]IEnumerable<IExternalAuthentication> providers, [FromServices]IUserClaimsPrincipalFactory<MembershipUser> claimsPrincipalFactory)
{
if (model == null) return BadRequest("Missing agrumets");
if (string.IsNullOrEmpty(model.Code)) return BadRequest($"{nameof(model.Code)} must not be empty");
if (string.IsNullOrEmpty(model.Provider)) return BadRequest($"{nameof(model.Provider)} must not be empty");
var provider = providers.FirstOrDefault(w => w.ProviderName == model.Provider);
if (provider == null) return BadRequest($"Provider for external login {model.Provider} is missing");
try
{
var externalLoginInfo = await provider.AuthenticateAsync(model.Code);
var user = await _userManager.FindByLoginAsync(externalLoginInfo.LoginProvider,
externalLoginInfo.ProviderKey);
user = user ?? await _membershipUserHandler.RegisterUser(externalLoginInfo);
var claimsPrincipal = await claimsPrincipalFactory.CreateAsync(user);
return Ok(claimsPrincipal);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
这是呼叫者服务:
public async Task<ClaimsPrincipal> GetUserByExternalLogin(ExternalSignModel model)
{
var uri = $"{BaseServiceUri}/getexternal";
var response = await _client.PostAsync(uri, model, new JsonMediaTypeFormatter());
var user = await response.Content.ReadAsAsync<ClaimsPrincipal>();
return user;
}
_client
仅仅是HttpClient
但是当我在呼叫者上执行return Ok(claimsPrincipal);
时,我得到400错误。
我做错了什么?如何发送ClaimsPrincipal?