Identity Server:访问客户端上ExternalLoginCallback中AuthorizationProeperties中设置的令牌/项

时间:2017-11-14 16:18:15

标签: .net-core identityserver4

问题

我有一个身份服务器实现,正在测试和生产中的许多应用程序中使用。我目前正在开发一项新功能,使用身份服务器的客户端应用程序可以执行Azure服务管理REST api调用。为此,它需要一个令牌。我可以生成此令牌,存储它,甚至可以在身份服务器的AccountController中访问它。

我的问题是弄清楚如何将此信息发送给客户。我不认为此令牌属于用户的声明。所以我尝试将它作为AuthenticationProperties的一部分添加为令牌,但我似乎无法在客户端中访问它。我应该将它存储在像这样的会话中吗SO用户link?这个问题有一个答案,但这似乎不对(我甚至出于绝望而尝试过它!)

代码的相关部分

生成令牌

var resource = "https://management.azure.com/";

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
     Events = new OpenIdConnectEvents
                {
                    OnAuthorizationCodeReceived = async context =>
                    {
                       // Acquire the token for the resource and save it
                    }
                }
}

AccountController

中恢复它
public async Task<IActionResult> ExternalLoginCallback(string returnUrl)
{
      string resource = "https://management.azure.com/";

      // snip
      result = await authContext.AcquireTokenSilentAsync(resource, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

      // snip

      AuthenticationProperties props = null;
      var tokens = new List<AuthenticationToken>();
      var id_token = info.Properties.GetTokenValue("id_token");
      if (id_token != null)
      {
          tokens.Add(new AuthenticationToken { Name = "id_token", Value = id_token });
      }

      if (result != null)
      {
          tokens.Add(new AuthenticationToken { Name = "management_token", Value = result.AccessToken });
      }

      if (tokens.Any())
      {
          props = new AuthenticationProperties();
          props.StoreTokens(tokens);
      }

     // snip
     // Can I access these "props" on the client? I even tried adding it to `Items`, no luck.
      await HttpContext.Authentication.SignInAsync(user.UserId, user.DisplayName, provider, props, additionalClaims.ToArray());
}

所以,我的问题是,这是正确的方法吗?如果是这样,我如何访问身份验证属性集?或者我应该尝试在Session中保存吗?如果是这样,我如何将其存储在客户的会话中?

任何指针都会有所帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

只想发布答案,以便希望获得答案的人们可以受益。

可以实现令牌缓存来实现此目的。 This存储库说明了如何。

要特别注意链接到hereAdalDistributedTokenCache