microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/PABot/Dialogs/LogoutDialog.cs
101lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Bot.Builder; |
| 5 | using Microsoft.Bot.Builder.Dialogs; |
| 6 | using Microsoft.Bot.Connector.Authentication; |
| 7 | using Microsoft.Bot.Schema; |
| 8 | |
| 9 | namespace PABot.Dialogs |
| 10 | { |
| 11 | /// <summary> |
| 12 | /// A dialog that handles user logout. |
| 13 | /// </summary> |
| 14 | public class LogoutDialog : ComponentDialog |
| 15 | { |
| 16 | /// <summary> |
| 17 | /// Initializes a new instance of the <see cref="LogoutDialog"/> class. |
| 18 | /// </summary> |
| 19 | /// <param name="id">The dialog ID.</param> |
| 20 | /// <param name="connectionName">The connection name configured in Azure Bot service.</param> |
| 21 | public LogoutDialog(string id, string connectionName) |
| 22 | : base(id) |
| 23 | { |
| 24 | ConnectionName = connectionName; |
| 25 | } |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Gets the configured connection name in Azure Bot service. |
| 29 | /// </summary> |
| 30 | protected string ConnectionName { get; } |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Called when the dialog is started and pushed onto the parent's dialog stack. |
| 34 | /// </summary> |
| 35 | /// <param name="innerDc">The inner DialogContext for the current turn of conversation.</param> |
| 36 | /// <param name="options">Initial information to pass to the dialog.</param> |
| 37 | /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> |
| 38 | /// <returns>A task representing the asynchronous operation.</returns> |
| 39 | protected override async Task<DialogTurnResult> OnBeginDialogAsync( |
| 40 | DialogContext innerDc, |
| 41 | object options, |
| 42 | CancellationToken cancellationToken = default(CancellationToken)) |
| 43 | { |
| 44 | DialogTurnResult result = await InterruptAsync(innerDc, cancellationToken); |
| 45 | if (result != null) |
| 46 | { |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | return await base.OnBeginDialogAsync(innerDc, options, cancellationToken); |
| 51 | } |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Called when the dialog is continued, where it is the active dialog and the user replies with a new activity. |
| 55 | /// </summary> |
| 56 | /// <param name="innerDc">The inner DialogContext for the current turn of conversation.</param> |
| 57 | /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> |
| 58 | /// <returns>A task representing the asynchronous operation.</returns> |
| 59 | protected override async Task<DialogTurnResult> OnContinueDialogAsync( |
| 60 | DialogContext innerDc, |
| 61 | CancellationToken cancellationToken = default(CancellationToken)) |
| 62 | { |
| 63 | DialogTurnResult result = await InterruptAsync(innerDc, cancellationToken); |
| 64 | if (result != null) |
| 65 | { |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | return await base.OnContinueDialogAsync(innerDc, cancellationToken); |
| 70 | } |
| 71 | |
| 72 | /// <summary> |
| 73 | /// Called when the dialog is interrupted, where it is the active dialog and the user replies with a new activity. |
| 74 | /// </summary> |
| 75 | /// <param name="innerDc">The inner DialogContext for the current turn of conversation.</param> |
| 76 | /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> |
| 77 | /// <returns>A task representing the asynchronous operation.</returns> |
| 78 | private async Task<DialogTurnResult> InterruptAsync( |
| 79 | DialogContext innerDc, |
| 80 | CancellationToken cancellationToken = default(CancellationToken)) |
| 81 | { |
| 82 | if (innerDc.Context.Activity.Type == ActivityTypes.Message) |
| 83 | { |
| 84 | string text = innerDc.Context.Activity.Text.ToLowerInvariant(); |
| 85 | |
| 86 | // Allow logout anywhere in the command |
| 87 | if (text.Contains("logout")) |
| 88 | { |
| 89 | // The UserTokenClient encapsulates the authentication processes. |
| 90 | UserTokenClient userTokenClient = innerDc.Context.TurnState.Get<UserTokenClient>(); |
| 91 | await userTokenClient.SignOutUserAsync(innerDc.Context.Activity.From.Id, ConnectionName, innerDc.Context.Activity.ChannelId, cancellationToken).ConfigureAwait(false); |
| 92 | |
| 93 | await innerDc.Context.SendActivityAsync(MessageFactory.Text("You have been signed out."), cancellationToken); |
| 94 | return await innerDc.CancelAllDialogsAsync(cancellationToken); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return null!; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |