Forum Discussion

Lakshmi_145's avatar
Lakshmi_145
Brass Contributor
Oct 20, 2023

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,
    Text = "Please Sign In",
    Title = "Sign In",
    Timeout = 300000, // User has 5 minutes to login (1000 * 60 * 5)
                }));

   AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));

   AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
   {
      PromptStepAsync,
      LoginStepAsync,
      DisplayTokenPhase1Async,
      DisplayTokenPhase2Async,
      }));

      // The initial child Dialog to run.
      InitialDialogId = nameof(WaterfallDialog);
    }

        private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            return await stepContext.BeginDialogAsync(nameof(OAuthPrompt), null, cancellationToken);
        }

        private async Task<DialogTurnResult> LoginStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // Get the token from the previous step. Note that we could also have gotten the
            // token directly from the prompt itself. There is an example of this in the next method.
            var tokenResponse = (TokenResponse)stepContext.Result;
            if (tokenResponse != null)
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("You are now logged in."), cancellationToken);
                return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to view your token?") }, cancellationToken);
            }

            await stepContext.Context.SendActivityAsync(MessageFactory.Text("Login was not successful please try again."), cancellationToken);
            return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
        }

 

Instead of sending the oauth prompt we need to send the oauth card as an attachment to another context and token validation should be done in the a new dialog.

public static OAuthCard GetOAuthCard()
{
            var oauthCard = new OAuthCard
            {
                Text = "BotFramework OAuth Card",
                ConnectionName = "OAuth connection", // Replace with the name of your Azure AD connection.
                Buttons = new List<CardAction> { new CardAction(ActionTypes.Signin, "Sign In", value: "https://example.org/signin") },
            };

            return oauthCard;
}

 

  • LeonPavesic's avatar
    LeonPavesic
    Silver 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)

Resources