-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance the sample to include Oauth Bot using FIC. (#4020)
- Loading branch information
1 parent
baf38f4
commit 84fab35
Showing
13 changed files
with
330 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
samples/csharp_dotnetcore/86.bot-authentication-fic/86.bot-authentication-fic.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.5.002.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuthFederatedCredBot", "AuthFederatedCredBot.csproj", "{0D47DE58-8F94-4E24-9265-77074A356DBE}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{0D47DE58-8F94-4E24-9265-77074A356DBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{0D47DE58-8F94-4E24-9265-77074A356DBE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{0D47DE58-8F94-4E24-9265-77074A356DBE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{0D47DE58-8F94-4E24-9265-77074A356DBE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {B623C9C4-F8D5-4AAC-AA92-FC01B93080ED} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
samples/csharp_dotnetcore/86.bot-authentication-fic/Bots/AuthBotFIC.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.22.0 | ||
|
||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Builder.Dialogs; | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.BotBuilderSamples.Bots | ||
{ | ||
public class AuthBotFIC<T> : DialogBot<T> where T : Dialog | ||
{ | ||
public AuthBotFIC(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger) | ||
: base(conversationState, userState, dialog, logger) | ||
{ | ||
} | ||
|
||
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) | ||
{ | ||
foreach (var member in turnContext.Activity.MembersAdded) | ||
{ | ||
if (member.Id != turnContext.Activity.Recipient.Id) | ||
{ | ||
await turnContext.SendActivityAsync(MessageFactory.Text("Welcome to AuthenticationBot using Federated Credentials. Type anything to get logged in. Type 'logout' to sign-out."), cancellationToken); | ||
} | ||
} | ||
} | ||
|
||
protected override async Task OnTokenResponseEventAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken) | ||
{ | ||
Logger.LogInformation("Running dialog with Token Response Event Activity."); | ||
|
||
// Run the Dialog with the new Token Response Event Activity. | ||
await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
samples/csharp_dotnetcore/86.bot-authentication-fic/Bots/DialogBot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Builder.Dialogs; | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.BotBuilderSamples | ||
{ | ||
// This IBot implementation can run any type of Dialog. The use of type parameterization is to allows multiple different bots | ||
// to be run at different endpoints within the same project. This can be achieved by defining distinct Controller types | ||
// each with dependency on distinct IBot types, this way ASP Dependency Injection can glue everything together without ambiguity. | ||
// The ConversationState is used by the Dialog system. The UserState isn't, however, it might have been used in a Dialog implementation, | ||
// and the requirement is that all BotState objects are saved at the end of a turn. | ||
public class DialogBot<T> : ActivityHandler where T : Dialog | ||
{ | ||
protected readonly BotState ConversationState; | ||
protected readonly Dialog Dialog; | ||
protected readonly ILogger Logger; | ||
protected readonly BotState UserState; | ||
|
||
public DialogBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger) | ||
{ | ||
ConversationState = conversationState; | ||
UserState = userState; | ||
Dialog = dialog; | ||
Logger = logger; | ||
} | ||
|
||
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
await base.OnTurnAsync(turnContext, cancellationToken); | ||
|
||
// Save any state changes that might have occurred during the turn. | ||
await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken); | ||
await UserState.SaveChangesAsync(turnContext, false, cancellationToken); | ||
} | ||
|
||
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) | ||
{ | ||
Logger.LogInformation("Running dialog with Message Activity."); | ||
|
||
// Run the Dialog with the new message Activity. | ||
await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken); | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
samples/csharp_dotnetcore/86.bot-authentication-fic/Bots/EchoBot.cs
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
samples/csharp_dotnetcore/86.bot-authentication-fic/Dialogs/LogoutDialog.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Builder.Dialogs; | ||
using Microsoft.Bot.Connector.Authentication; | ||
using Microsoft.Bot.Schema; | ||
|
||
namespace Microsoft.BotBuilderSamples | ||
{ | ||
public class LogoutDialog : ComponentDialog | ||
{ | ||
public LogoutDialog(string id, string connectionName) | ||
: base(id) | ||
{ | ||
ConnectionName = connectionName; | ||
} | ||
|
||
protected string ConnectionName { get; } | ||
|
||
protected override async Task<DialogTurnResult> OnBeginDialogAsync(DialogContext innerDc, object options, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var result = await InterruptAsync(innerDc, cancellationToken); | ||
if (result != null) | ||
{ | ||
return result; | ||
} | ||
|
||
return await base.OnBeginDialogAsync(innerDc, options, cancellationToken); | ||
} | ||
|
||
protected override async Task<DialogTurnResult> OnContinueDialogAsync(DialogContext innerDc, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var result = await InterruptAsync(innerDc, cancellationToken); | ||
if (result != null) | ||
{ | ||
return result; | ||
} | ||
|
||
return await base.OnContinueDialogAsync(innerDc, cancellationToken); | ||
} | ||
|
||
private async Task<DialogTurnResult> InterruptAsync(DialogContext innerDc, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
if (innerDc.Context.Activity.Type == ActivityTypes.Message) | ||
{ | ||
var text = innerDc.Context.Activity.Text.ToLowerInvariant(); | ||
|
||
if (text == "logout") | ||
{ | ||
// The UserTokenClient encapsulates the authentication processes. | ||
var userTokenClient = innerDc.Context.TurnState.Get<UserTokenClient>(); | ||
await userTokenClient.SignOutUserAsync(innerDc.Context.Activity.From.Id, ConnectionName, innerDc.Context.Activity.ChannelId, cancellationToken).ConfigureAwait(false); | ||
|
||
await innerDc.Context.SendActivityAsync(MessageFactory.Text("You have been signed out."), cancellationToken); | ||
return await innerDc.CancelAllDialogsAsync(cancellationToken); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.