问题描述
我认为这与导致错误的json网络令牌有关。我认为我可能错误地创建了json网络令牌,但不能100%确定。如果我确实创建了错误的文件,则不确定在哪里弄错。我被困在这一部分。非常感谢您的帮助。
另一方面,这是一个使用.NET Core 3.1的Microsoft MVC Web应用程序项目。这是用于连接到Microsoft Health Bot的Web聊天。客户端javascript代码基于Health Bot Container Sample代码。
目标: 通过javascript代码主动触发Microsoft Health Bot方案。
在客户端收到错误响应: 加载资源失败:服务器响应状态为502()。
{
"error": {
"code": "BadArgument","message": "Missing token or secret"
}
}
服务器端C#代码:
using ........
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
private string GenerateJsonWebToken()
{
// Using the direct line secret key,create a new
// symmetric security key instance.
var tmpSecretKey = Encoding.UTF8.GetBytes("secretKey");
SymmetricSecurityKey tmpSecurityKey = new SymmetricSecurityKey(tmpSecretKey);
// Using the symmetric security key,create a new
// signing credential instance.
SigningCredentials tmpSigningCreds = new SigningCredentials(tmpSecurityKey,SecurityAlgorithms.HmacSha512Signature);
// New claims collection that contains some user info.
List<Claim> tmpClaims = new List<Claim>();
tmpClaims.Add(new Claim(JwtRegisteredClaimNames.Jti,"userId"));
tmpClaims.Add(new Claim(JwtRegisteredClaimNames.Sub,"userName"));
// JWT security token instance.
JwtSecurityToken tmpSecurityToken = new JwtSecurityToken(claims: tmpClaims,signingCredentials: tmpSigningCreds);
// JWT security token handler to create json web tokens.
JwtSecurityTokenHandler tmpSecurityTokenHandler = new JwtSecurityTokenHandler();
string tmpResult = tmpSecurityTokenHandler.Writetoken(tmpSecurityToken);
return tmpResult;
}
public IActionResult Index()
{
// Security token.
ViewBag.JsonWebToken = GenerateJsonWebToken();
// Scenario to launch when the web chat is launched.
ViewBag.AutomaticWelcomeScenario = this._automaticWelcomeScenario;
return View();
}
客户端Javascript代码:
const tmpJWT = "@ViewBag.JsonWebToken";
// Create our own store where we Could specify a scenario to use as an automatic welcome scenario
// to greet the user when the web chat is first started up.
// Sample code:
// https://github.com/microsoft/HealthBotContainerSample/blob/master/public/index.js
var tmpStore = window.WebChat.createStore({},function (store) {
return function (next) {
return function (action) {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
store.dispatch({
type: 'DIRECT_LINE/POST_ACTIVITY',Meta: { method: 'keyboard' },payload: {
activity: {
type: "invoke",name: "InitConversation",locale: "en-US",value: {
// Must use for authenticated conversation.
jsonWebToken: tmpJWT,// Use the following activity to proactively invoke a bot scenario.
triggeredScenario: {
trigger: "@ViewBag.AutomaticWelcomeScenario",args: {
myVariable1: "Test Value 1",myVariable2: "Test Value 2"
}
}
}
}
}
});
}
return next(action);
}
}
});
解决方法
我能够在Microsoft的帮助下使用它。因为聊天对话不是经过身份验证的对话,所以我不需要json网络令牌。在我取出json网络令牌的用法后,它开始工作了。在启动网络聊天时,我能够通过代码加载特定的场景。