private async Task InterceptOAuthCardsAsync()

in templates/csharp/VA/VA/TokenExchange/TokenExchangeSkillHandler.cs [90:135]


        private async Task<bool> InterceptOAuthCardsAsync(ClaimsIdentity claimsIdentity, Activity activity)
        {
            if (activity.Attachments != null)
            {
                BotFrameworkSkill targetSkill = null;
                foreach (var attachment in activity.Attachments.Where(a => a?.ContentType == OAuthCard.ContentType))
                {
                    if (targetSkill == null)
                    {
                        targetSkill = GetCallingSkill(claimsIdentity);
                    }

                    if (targetSkill != null)
                    {
                        var oauthCard = ((JObject)attachment.Content).ToObject<OAuthCard>();

                        if (oauthCard != null && oauthCard.TokenExchangeResource != null &&
                            _tokenExchangeConfig != null && !string.IsNullOrWhiteSpace(_tokenExchangeConfig.Provider) &&
                            _tokenExchangeConfig.Provider == oauthCard.TokenExchangeResource.ProviderId)
                        {
                            using (var context = new TurnContext(_adapter, activity))
                            {
                                context.TurnState.Add<IIdentity>(BotAdapter.BotIdentityKey, claimsIdentity);

                                // AAD token exchange
                                var result = await _tokenExchangeProvider.ExchangeTokenAsync(
                                    context,
                                    _tokenExchangeConfig.ConnectionName,
                                    activity.Recipient?.Id,
                                    new TokenExchangeRequest() { Uri = oauthCard.TokenExchangeResource.Uri }).ConfigureAwait(false);

                                if (!string.IsNullOrEmpty(result.Token))
                                {
                                    // Send an Invoke back to the Skill
                                    return await SendTokenExchangeInvokeToSkill(activity, oauthCard.TokenExchangeResource.Id, result.Token, oauthCard.ConnectionName, targetSkill, default).ConfigureAwait(false);
                                }

                                return false;
                            }
                        }
                    }
                }
            }

            return false;
        }