.net core Identity Server:访问客户端ExternalLoginCallback中AuthorizationProeperties中设置的令牌/项目 问题:

.net core Identity Server:访问客户端ExternalLoginCallback中AuthorizationProeperties中设置的令牌/项目 问题:,.net-core,identityserver4,.net Core,Identityserver4,我有一个identity server实现,它正在测试和生产中被许多应用程序使用。我目前正在开发一个新功能,使用identity server的客户端应用程序可以执行Azure服务管理REST api调用。为此,它需要一个令牌。我可以生成这个令牌,存储它,甚至在identity server的AccountController中访问它 我的问题是如何把这个发送给客户。我认为这个令牌不属于用户的声明。因此,我尝试将其作为AuthenticationProperties的一部分添加为令牌,但似乎无法

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

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

守则的有关章节 生成令牌

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());
}
public异步任务ExternalLoginCallback(string returnUrl)
{
字符串资源=”https://management.azure.com/";
//剪断
结果=等待authContext.AcquireTokenSilentAsync(资源、凭证、新用户标识符(userObjectID、UserIdentifierType.UniqueId));
//剪断
AuthenticationProperties props=null;
var tokens=新列表();
var id_token=info.Properties.GetTokenValue(“id_token”);
if(id\u令牌!=null)
{
添加(新的AuthenticationToken{Name=“id\u token”,Value=id\u token});
}
如果(结果!=null)
{
添加(新的AuthenticationToken{Name=“management_token”,Value=result.AccessToken});
}
if(tokens.Any())
{
props=新的AuthenticationProperties();
道具。商店代币(代币);
}
//剪断
//我可以在客户端上访问这些“道具”吗?我甚至尝试将其添加到“项目”中,但运气不佳。
等待HttpContext.Authentication.SignInAsync(user.UserId、user.DisplayName、provider、props、additionalClaims.ToArray());
}
那么,我的问题是,这是正确的方式吗?如果是,如何访问身份验证属性集?或者我应该尝试将其保存在
会话中
?如果是,如何将其存储在客户端会话中


任何指示都会有帮助。谢谢大家!

只是想发布一个答案,让想要同样答案的人也能从中受益

可以实现令牌缓存来实现这一点。存储库解释了如何


请特别注意链接的
AdalDistributedTokenCache

是否有幸在k25找到解决方案?我正在处理一个类似的问题@Amirchartbahr我发布了一个答案,说明了如何进行。希望这有帮助!