如何知道哪个用户已使用C#在Bot Framework v4中进行了身份验证?

问题描述 投票:0回答:1

我有一个使用c#框架v4制作的机器人。它已经使用天蓝色广告进行了身份验证。用户列表已添加到azure广告中。用户登录后,是否有任何方法可以知道哪个用户已登录。我们可以在bot中知道用户的名称或电子邮件ID。

我已点击此链接以对带有广告的机器人进行身份验证。

https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-authentication?view=azure-bot-service-4.0&tabs=csharp

仿真器中显示的图像:

enter image description here代码

public class MainDialog : ComponentDialog
{
private readonly IBotServices _botServices;
protected readonly ILogger _logger;
private readonly UserState _userState;

private readonly string _connectionName;

private readonly IConfiguration _configuration;
public MainDialog(IConfiguration configuration,ILogger<MainDialog> logger, IBotServices botServices)
    : base(nameof(MainDialog))
{
    _configuration = configuration;
    _logger = logger;
    _botServices = botServices ?? throw new System.ArgumentNullException(nameof(botServices));
    _connectionName = configuration["ConnectionName"];

    AddDialog(new OAuthPrompt(
      nameof(OAuthPrompt),
      new OAuthPromptSettings
      {
          ConnectionName = configuration["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 ChoicePrompt(nameof(ChoicePrompt)));
    AddDialog(new luisandqnamakerDialog(_botServices,_configuration,_logger));
    AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
    {
        PromptStepAsync,
        LoginStepAsync             
    }));

    // 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)
    {
        if (IsAuthCodeStep(stepContext.Context.Activity.Text))
        {
            await stepContext.Context.SendActivityAsync(MessageFactory.Text("You are now logged in."), cancellationToken);
            return await stepContext.NextAsync();
        }
        else
        {
             await stepContext.PromptAsync(nameof(luisandqnamakerDialog), new PromptOptions { Prompt = MessageFactory.Text("Would you like to ask your question?") }, cancellationToken);
            return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
        }
    }           

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

    return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
}

private bool IsAuthCodeStep(string code)
{
    if (string.IsNullOrEmpty(code) || !code.Length.Equals(6)) return false;
    if (!int.TryParse(code, out int result)) return false;
    if (result > 1) return true;                
    return false;
}

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 bot adapter encapsulates the authentication processes.
            var botAdapter = (BotFrameworkAdapter)innerDc.Context.Adapter;
            await botAdapter.SignOutUserAsync(innerDc.Context, _connectionName, null, cancellationToken);
            await innerDc.Context.SendActivityAsync(MessageFactory.Text("You have been signed out."), cancellationToken);
            return await innerDc.CancelAllDialogsAsync(cancellationToken);
        }
    }

    return null;
}
}
c# asp.net-core azure-active-directory botframework luis
1个回答
0
投票

您正在遵循正确的文档。如果查看示例24.bot-authentication-msgraph,则Bot Framework v4机器人程序身份验证正在使用MS Graph示例。此示例利用Graph API检索有关用户的数据。一旦设置了AADv2应用程序并添加了作用域,就可以在仿真器上本地测试bot示例。登录后,如果键入'me',它将返回用户名。

以下是示例在仿真器中的工作方式示例:enter image description here

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.