[英]Azure Web Chat Bot Token Server
I am struggeling to understand how to get tokens.我正在努力了解如何获取令牌。 I know why I should use them, but I just don't understand how to get them.
我知道为什么我应该使用它们,但我就是不明白如何获得它们。 All the samples that uses Tokens just fetch them from " https://webchat-mockbot.azurewebsites.net/directline/token " or something similar.
所有使用令牌的示例都只是从“ https://webchat-mockbot.azurewebsites.net/directline/token ”或类似的东西中获取它们。 How do I create this path in my bot?
如何在我的机器人中创建此路径?
Describe alternatives you have considered描述你考虑过的替代方案
I was able to create something which worked with my JS-Bot:我能够创建一些与我的 JS-Bot 一起使用的东西:
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
console.log('\nTo talk to your bot, open the emulator select "Open Bot"');
});
server.post('/token-generate', async (_, res) => {
console.log('requesting token ');
try {
const cres = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
headers: {
authorization: `Bearer ${ process.env.DIRECT_LINE_SECRET }`
},
method: 'POST'
});
const json = await cres.json();
if ('error' in json) {
res.send(500);
} else {
res.send(json);
}
} catch (err) {
res.send(500);
}
});
But I don't find how to do this with my C#-Bot ( I switched to C# because I understand it better than JS).但是我没有找到如何用我的 C#-Bot 做到这一点(我切换到 C#,因为我比 JS 更了解它)。
In my C#-Bot there is only this:在我的 C#-Bot 中只有这个:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
namespace ComplianceBot.Controllers
{
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
// achieved by specifying a more specific type for the bot constructor argument.
[Route("api/messages")]
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly IBot _bot;
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
_adapter = adapter;
_bot = bot;
}
[HttpGet, HttpPost]
public async Task PostAsync()
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await _adapter.ProcessAsync(Request, Response, _bot);
}
}
}
Can I add a new Route here?我可以在这里添加一条新路线吗? like [Route("directline/token")] ?
像 [Route("directline/token")] ?
I know I could do this with an extra "token-server" (I don't know how to realise it, but I know that would work), but if possible I'd like to do this with my already existing c#-bot as I did it with my JS-Bot.我知道我可以用一个额外的“令牌服务器”来做到这一点(我不知道如何实现它,但我知道这会起作用),但如果可能的话,我想用我已经存在的 c#-bot 来做到这一点就像我用我的 JS-Bot 做的那样。
I have posted an answer which includes how to implement an API to get a direct line access token in C# bot and how to get this token, just refer to here .我已经发布了一个答案,其中包括如何在 C# bot 中实现 API 以获取直接线路访问令牌以及如何获取此令牌, 请参阅此处。 If you have any further questions, pls feel free to let me know .
如果您有任何其他问题,请随时告诉我。
Update :更新 :
My code is based on this demo .我的代码基于此演示。 If you are using .net core, pls create a
TokenController.cs
under /Controllers
folder:如果您使用的是 .net core,请在
/Controllers
文件夹下创建一个TokenController.cs
:
Code of TokenController.cs
: TokenController.cs
代码:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Microsoft.BotBuilderSamples.Controllers
{
[Route("api/token")]
[ApiController]
public class TokenController : ControllerBase
{
[HttpGet]
public async Task<ObjectResult> getToken()
{
var secret = "<direct line secret here>";
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Post,
$"https://directline.botframework.com/v3/directline/tokens/generate");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secret);
var userId = $"dl_{Guid.NewGuid()}";
request.Content = new StringContent(
Newtonsoft.Json.JsonConvert.SerializeObject(
new { User = new { Id = userId } }),
Encoding.UTF8,
"application/json");
var response = await client.SendAsync(request);
string token = String.Empty;
if (response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();
token = JsonConvert.DeserializeObject<DirectLineToken>(body).token;
}
var config = new ChatConfig()
{
token = token,
userId = userId
};
return Ok(config);
}
}
public class DirectLineToken
{
public string conversationId { get; set; }
public string token { get; set; }
public int expires_in { get; set; }
}
public class ChatConfig
{
public string token { get; set; }
public string userId { get; set; }
}
}
Run the project after you replace secret with your own direct line secret.用您自己的直线机密替换机密后运行该项目。 You will be able to get token by url:
http://localhost:3978/api/token
on local :您将能够通过 url 获取令牌:
http://localhost:3978/api/token
on local :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.