in libraries/Microsoft.Bot.Builder.Dialogs/Prompts/OAuthPrompt.cs [465:530]
public override async Task<DialogTurnResult> ContinueDialogAsync(DialogContext dc, CancellationToken cancellationToken = default)
{
if (dc == null)
{
throw new ArgumentNullException(nameof(dc));
}
// Check for timeout
var state = dc.ActiveDialog.State;
var expires = (DateTime)state[PersistedExpires];
var isMessage = dc.Context.Activity.Type == ActivityTypes.Message;
// If the incoming Activity is a message, or an Activity Type normally handled by OAuthPrompt,
// check to see if this OAuthPrompt Expiration has elapsed, and end the dialog if so.
var isTimeoutActivityType = isMessage
|| IsTokenResponseEvent(dc.Context)
|| IsTeamsVerificationInvoke(dc.Context)
|| IsTokenExchangeRequestInvoke(dc.Context);
var hasTimedOut = isTimeoutActivityType && DateTime.Compare(DateTime.UtcNow, expires) > 0;
if (hasTimedOut)
{
// if the token fetch request times out, complete the prompt with no result.
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
// Recognize token
var recognized = await RecognizeTokenAsync(_settings, dc, cancellationToken).ConfigureAwait(false);
var promptState = state[PersistedState].CastTo<IDictionary<string, object>>();
var promptOptions = state[PersistedOptions].CastTo<PromptOptions>();
// Increment attempt count
// Convert.ToInt32 For issue https://github.com/Microsoft/botbuilder-dotnet/issues/1859
promptState[Prompt<int>.AttemptCountKey] = Convert.ToInt32(promptState[Prompt<int>.AttemptCountKey], CultureInfo.InvariantCulture) + 1;
// Validate the return value
var isValid = false;
if (_validator != null)
{
var promptContext = new PromptValidatorContext<TokenResponse>(dc.Context, recognized, promptState, promptOptions);
isValid = await _validator(promptContext, cancellationToken).ConfigureAwait(false);
}
else if (recognized.Succeeded)
{
isValid = true;
}
// Return recognized value or re-prompt
if (isValid)
{
return await dc.EndDialogAsync(recognized.Value, cancellationToken).ConfigureAwait(false);
}
else if (isMessage && _settings.EndOnInvalidMessage)
{
// If EndOnInvalidMessage is set, complete the prompt with no result.
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
if (!dc.Context.Responded && isMessage && promptOptions?.RetryPrompt != null)
{
await dc.Context.SendActivityAsync(promptOptions.RetryPrompt, cancellationToken).ConfigureAwait(false);
}
return EndOfTurn;
}