Skip to content

Add CompatTeamsSSOTokenExchangeMiddleware#436

Open
rido-min wants to merge 2 commits intonext/corefrom
next/core-compat-sso-middleware
Open

Add CompatTeamsSSOTokenExchangeMiddleware#436
rido-min wants to merge 2 commits intonext/corefrom
next/core-compat-sso-middleware

Conversation

@rido-min
Copy link
Copy Markdown
Member

Summary

  • Adds CompatTeamsSSOTokenExchangeMiddleware as a drop-in replacement for the BF SDK's TeamsSSOTokenExchangeMiddleware that works with the Teams Bot Core SDK via CompatUserTokenClient
  • Fixes System.InvalidCastException: Unable to cast object of type 'CompatConversations' to type 'Microsoft.Bot.Connector.Conversations' thrown when BF SDK's TeamsInfo.GetMemberAsync is invoked through TeamsActivityHandler.OnTeamsMembersAddedDispatchAsync
  • Calling code should use CompatTeamsSSOTokenExchangeMiddleware + CompatTeamsInfo instead of the BF SDK equivalents

Error details

System.InvalidCastException
  Message=Unable to cast object of type 'Microsoft.Teams.Bot.Compat.CompatConversations'
          to type 'Microsoft.Bot.Connector.Conversations'.
  StackTrace:
   at Microsoft.Bot.Builder.Teams.TeamsInfo.<GetMemberAsync>d__22.MoveNext()
   at Microsoft.Bot.Builder.Teams.TeamsActivityHandler.<OnTeamsMembersAddedDispatchAsync>d__27.MoveNext()
   at Microsoft.Bot.Builder.ActivityHandler.<OnTurnAsync>d__0.MoveNext()
   at Microsoft.Bot.Builder.Teams.TeamsSSOTokenExchangeMiddleware.<OnTurnAsync>d__3.MoveNext()

The BF SDK's TeamsInfo.GetMemberAsync casts IConversations to the concrete Conversations class. Since CompatConversations only implements IConversations (not extending the concrete class), this cast fails. The compat middleware avoids triggering BF SDK code paths that perform this cast.

Test plan

  • Register CompatTeamsSSOTokenExchangeMiddleware in place of TeamsSSOTokenExchangeMiddleware in an SSO sample bot
  • Use CompatTeamsInfo instead of TeamsInfo in bot handlers
  • Verify SSO token exchange flow completes without InvalidCastException
  • Verify deduplication logic works for multi-client signin scenarios

🤖 Generated with Claude Code

The BF SDK's TeamsSSOTokenExchangeMiddleware triggers code paths through
TeamsActivityHandler.OnTeamsMembersAddedDispatchAsync that call
TeamsInfo.GetMemberAsync, which casts IConversations to the concrete
Conversations class. This fails with InvalidCastException because
CompatConversations only implements IConversations:

  System.InvalidCastException: Unable to cast object of type
  'Microsoft.Teams.Bot.Compat.CompatConversations' to type
  'Microsoft.Bot.Connector.Conversations'.

This adds CompatTeamsSSOTokenExchangeMiddleware as a drop-in replacement
that works with CompatUserTokenClient. Calling code should use this
middleware and CompatTeamsInfo instead of the BF SDK equivalents.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@rido-min rido-min added the CORE label Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant