如何通过 Microsoft Bot Framework 中的直线将我的用户 ID 和令牌从我的客户端发送到 Bot

How can I send my userID an Token from my client to the Bot through direct line in Microsoft Bot Framework

我有一个通过登录进行身份验证的客户端,我需要在每个请求中将该用户 ID 和令牌传递给机器人,以便我可以使用它来访问需要此信息的服务器 Web api。我尝试获取 header 中的授权,但它带有秘密。如果我可以将 id 和 token 附加到 header.

对我有用
this.botConnection = new BotChat.DirectLine({
    secret: 'yNy9g1Mok1g.cwA.fyks.fghsth.rcKOQUIu558-TI',//params['mysecret'], 
    token: params[this.token],     //I think I should pass it here but this does not work.
  //domain: params['ngroktunneledurl.com/api/messages'],
    webSocket: params['webSocket'] && params['webSocket'] === "true" // defaults to true,
});

然后从机器人尝试检索消息控制器中的令牌..

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {

        if (activity.Type == ActivityTypes.Message)
        {
          var keyValuePair = Request.Headers.FirstOrDefault(h => h.Key.Equals("Authorization"));
          token = keyValuePair.Value.First().Substring(7);

           await Conversation.SendAsync(activity, () => new Dialogs.RootDialog(token));
        }
        else
        {
            HandleSystemMessage(activity).ConfigureAwait(false);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

授权带来了识别我们的机器人的秘密。

ChannelData 不会被连接器服务剥离。尽管此 属性 旨在提供通道特定功能,但它也可用于将额外信息从 bot 客户端传递给 bot:https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-channeldata

您可以将您的令牌值或任何类型的字符串传递给 id 并将其与标识符连接,以便您可以拆分内部代码并将其作为数组获取。

使用下面的代码:

<div id="bot"></div>
<script>
    var spCOntext;
    var FirstName;
    var emailaddress; 
    var Environment;
    try{
        spCOntext = "your context";
        FirstName =  _spPageContextInfo.userDisplayName;
        emailaddress = "your email";
        Environment= "application URL";
    }
    catch(ex)
    {
        spCOntext = 'You';
        Environment = 'Local System';

    }

    BotChat.App({

        directLine: { secret: 'Your secret' },
        //directLine: { secret: 'Your secret' },
        user: { id: FirstName ,Name: spCOntext+'*'+Environment},
        bot: { id: 'your bot id' },
        resize: 'detect'
    }, document.getElementById("bot"));

</script>

您可以像这样在您的机器人 C# 代码中获取值:

public async Task AltairQueryResponse(IDialogContext context, IAwaitable<IMessageActivity> response)
    {
        var message = await response;
        var getContext = message.From.Name.Split('*')[0];// get context
        var Environment= message.From.Name.Split('*')[0];// get Environment
        await context.PostAsync(getContext);
        await context.PostAsync(Environment);
    }

如果您需要任何其他帮助,请告诉我

我可以通过向包含我需要发送的值的机器人发送 activity 来实现这一点。我只需要反序列化对象,然后从 C# 代码中获取它的值。

BotChat.App({
        botConnection:
        {
            ...dl,
            postActivity: activity => {
                // Add some custom data
                activity.channelData.MyKey = this.Id;
                activity.channelData.token = this.token;
                return dl.postActivity(activity)
            }
        },
          user: user,
          bot: bot
      }, document.getElementById("BotChatGoesHere"))