Forum Discussion
Lakshmi_145
Oct 20, 2023Iron Contributor
Send OauthPrompt as a Oauth Card
Currently, we are using a graph client for permissions of user sign in and using the below code for Sign in and token validation new OAuthPromptSettings
{
ConnectionName = ConnectionName,
...
LeonPavesic
Oct 23, 2023Silver Contributor
Hi Lakshmi_145,
here's the example code for sending an OAuth card as an attachment and performing token validation in a new dialog with explanation, maybe you can try it for your issue:
// Step 1: Create an OAuth card using the OAuthCard class
private OAuthCard GetOAuthCard()
{
var oauthCard = new OAuthCard
{
Text = "BotFramework OAuth Card",
ConnectionName = "OAuth connection", // Replace with your connection name.
Buttons = new List<CardAction> { new CardAction(ActionTypes.Signin, "Sign In", value: "https://example.org/signin") },
};
return oauthCard;
}
// Step 2: Send the OAuth card as an attachment in a waterfall step
private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var oauthCard = GetOAuthCard();
var message = MessageFactory.Attachment(oauthCard.ToAttachment());
await stepContext.Context.SendActivityAsync(message, cancellationToken);
return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions
{
Prompt = MessageFactory.Text("Would you like to view your token?")
}, cancellationToken);
}
// Step 3: Define an OAuthPrompt for token validation
var oauthPrompt = new OAuthPrompt(nameof(OAuthPrompt), new OAuthPromptSettings
{
ConnectionName = ConnectionName,
Text = "Please Sign In",
Title = "Sign In",
Timeout = 300000, // User has 5 minutes to login (1000 * 60 * 5)
});
// Step 4: Create a new dialog for token validation
AddDialog(oauthPrompt);
AddDialog(new WaterfallDialog(nameof(ValidateTokenDialog), new WaterfallStep[]
{
// Step 5: Handle the token response in the new dialog
async (stepContext, cancellationToken) =>
{
return await stepContext.BeginDialogAsync(nameof(OAuthPrompt), null, cancellationToken);
},
async (stepContext, cancellationToken) =>
{
var tokenResponse = (TokenResponse)stepContext.Result;
if (tokenResponse != null)
{
// Token validation was successful; use the token for further actions.
await stepContext.Context.SendActivityAsync(MessageFactory.Text("Token validation successful."), cancellationToken);
}
else
{
await stepContext.Context.SendActivityAsync(MessageFactory.Text("Token validation failed. Please try again."), cancellationToken);
}
return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
}
}));
// Set the initial child dialog to run
InitialDialogId = nameof(ValidateTokenDialog);
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)