首页 文章

IdentityServer混合流 - 用户成功登录后访问令牌为空

提问于
浏览
1

我在检索经过身份验证的用户的访问令牌方面遇到了问题 . 下面是我的配置

ASP.NET MVC 5客户端:

  • OpenIdConnect

  • IdentityServer3库

  • ResponseType = "code id_token"

ASP.NET核心标识服务器:

  • IdentityServer4库

  • 客户端配置:AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

我正在尝试使用以下方法在我的客户端获取访问令牌:

AuthorizationCodeReceived = async n =>
{
    // use the code to get the access and refresh token
    var tokenClient = new TokenClient(TokenEndpoint, "clientid", "secret");
    var response = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri);      

},

我在上面的实现中使用了这个参考 - https://github.com/IdentityServer/IdentityServer3/issues/2457

response 中的属性具有空值 . 我需要访问令牌,以便登录客户端的用户可以访问api . 以下是我尝试检索访问令牌的另一种方法:

public async Task<ActionResult> CallApiUsingUserAccessToken()
{
    var user = User as ClaimsPrincipal;
    var accessToken = user.FindFirst("access_token").Value;

    var client = new HttpClient();
    client.SetBearerToken(accessToken);
    var content = await client.GetStringAsync("http://localhost:6001/api/values");

    ViewBag.Json = JArray.Parse(content).ToString();
    return View("json");
}

但是, user.FindFirst("access_token").Value; 为空 . 我已经在asp.net核心中尝试过IdentityServer4版本,但这似乎是我的一大迁移 . 谢谢 .

[updated]

我从未想到IdentityServer3中的 endpoints 与IDS4不同 . 我确实必须将 var tokenClient = new TokenClient(TokenEndpoint, "client", "secret"); 更改为 var tokenClient = new TokenClient("http://localhost:9000/connect/token", "client", "secret") ,因为IDS3中的TokenEndpoint是http://localhost:9000/core/connect/token,IDS4中不存在 endpoints "core" . 我能够在这一行获得访问令牌 var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri); 但是在授权之后,我仍然得到这个 var accessToken = user.FindFirst("access_token").Value; 代码行的空引用异常 .

2 回答

相关问题