My goal is to get an SSO token in a Teams bot. The code below is my try to get this working in the most basic way.
In response to any input, a waterfall dialog is executed which runs TeamsBotSsoPrompt
. I verified all settings are correct and there is no exception.
我的目标是在团队机器人中获得SSO令牌。下面的代码是我试图让它以最基本的方式工作。响应任何输入,将执行一个瀑布对话框来运行TeamsBotSsoPrompt。我验证了所有设置都是正确的,没有例外。
The line return await ssoPrompt.BeginDialogAsync(stepContext, _dialogState, cancellationToken);
executes without error.
返回的行等待ssoPrompt.BeginDialogAsync(SteContext,_DialogState,ancellationToken);执行时没有错误。
Stepping into the code I find that TeamsBotSsoPrompt
calls await SendOAuthCardToObtainTokenAsync(dialogContext.get_Context(), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
and that's the last event I see. There is no output in Teams.
进入代码,我发现TeamsBotSsoPrompt调用等待SendOAuthCardToObtainTokenAsync(dialogContext.get_Context(),cancellationToken).ConfigureAwait(continueOnCapturedContext:为False);这是我看到的最后一个事件。在团队中没有产出。
The last sign of activity in my bot is then a call to my override OnTeamsSigninVerifyStateAsync()
.
然后,我的bot中的最后一个活动标志是对我的覆盖OnTeamsSigninVerifyStateAsync()的调用。
The second step of the waterfall where I would get access to the token is never reached.
瀑布的第二个步骤,我将访问令牌的地方永远不会到达。
What do I have to do to get the token?
我要做什么才能拿到令牌?
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Builder.Dialogs;
using DataPlatformBot.SSO;
using Microsoft.TeamsFx.Bot;
using Microsoft.Extensions.Options;
using Microsoft.TeamsFx.Configuration;
namespace DataPlatformBot;
public class TeamsBot : TeamsActivityHandler
{
readonly ILogger<TeamsBot> _logger;
readonly SsoDialog _ssoDialog;
readonly IStatePropertyAccessor<DialogState> _dialogState;
readonly BotAuthenticationOptions _botAuthOptions;
readonly BotState _conversationState;
public TeamsBot(ConversationState conversationState, IOptions<BotAuthenticationOptions> botAuthOptions, SsoDialog ssoDialog, ILogger<TeamsBot> logger)
{
_ssoDialog = ssoDialog;
_logger = logger;
_conversationState = conversationState;
_dialogState = _conversationState.CreateProperty<DialogState>("DialogState");
_botAuthOptions = botAuthOptions.Value;
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var wd = new WaterfallDialog("SsoWaterfall", new List<WaterfallStep> {
async (stepContext, cancellationToken) => {
var ssoSettings = new TeamsBotSsoPromptSettings(_botAuthOptions, new string[] { "User.Read" });
var ssoPrompt = new TeamsBotSsoPrompt(nameof(TeamsBotSsoPrompt), ssoSettings);
return await ssoPrompt.BeginDialogAsync(stepContext, _dialogState, cancellationToken);
},
async (stepContext, cancellationToken) => {
var tokenResponse = (TeamsBotSsoPromptTokenResponse)stepContext.Result;
return await stepContext.EndDialogAsync();
}
});
await wd.RunAsync(turnContext, _dialogState, cancellationToken);
}
protected override async Task OnTeamsSigninVerifyStateAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken)
{
_logger.LogInformation("Receive invoke activity of teams sign in verify state");
}
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
await base.OnTurnAsync(turnContext, cancellationToken);
}
}
更多回答
我是一名优秀的程序员,十分优秀!