.net核心发送ClaimsPrincipal作为操作的响应

时间:2018-09-25 14:39:11

标签: c# asp.net-core asp.net-core-webapi dotnet-httpclient claims-based-identity

我正在尝试将用户数据作为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?

0 个答案:

没有答案