有几种方法可以解决您的问题。
据我了解,当客户端到达带有聊天机器人小部件的网页时,用户已经通过网页对您的网站进行了身份验证。
编辑:我添加了第三种方法,这是在网页和嵌入式 WebChat 框之间进行通信的首选方式。
方法一:
将用户凭据(身份验证令牌)传输到页面内的聊天机器人的一种方法是使用来自服务器身份验证端点的凭据开始与用户的新对话。
但是,为了使其工作,您需要用户的 IAddress。换句话说,用户之前必须与您的机器人进行过交谈,而您必须将其存储在某个地方,也许是在数据库中。
例如,这将是您的服务器代码(在 NodeJS 中):
//where user is sent after authenticating through web page
server.post("/authenticated", function(req, res){
//get user iaddress from database using credentials
//(you will need the Iaddress of the user to begin a dialog)
var address = getAddressFromDB(req.body.credentials)
//then use beginDialog to start a new dialog with the user you just authenticated
//(this dialog route will only be accessible from here to guarantee authentication)
bot.beginDialog(address, "/authenticated", { userAuthToken: auth_token });
//success
res.status(200);
res.end();
});
//user is sent here from the above bot.beginDialog() call
bot.dialog("/authenticated", function(session, args){
//extract credentials (auth token) from args
var authToken = args.userAuthToken;
//use auth token here.......
});
然后,您将执行正常逻辑,在机器人对话端点创建和处理支持票证。如果在以后的对话路由中需要,您甚至可以将 authToken 存储在 session 对象中。
session.userData.authToken = authToken;
方法二:
另一种验证用户身份的方法是通过聊天窗口本身通过瀑布对话框。 但是, 这并不能真正解决您让用户进行两次身份验证的问题,但可以解决用户必须离开当前网页进行身份验证的问题。
机器人将引导用户完成输入凭据的过程:
//begin user authentication here
bot.dialog("/get_username", [
function(session){
//prompt user for username here
botbuilder.Prompts.text(session, "Hello, what is your username?");
},
function(session, results){
//store username
session.userData.username = results.response;
//begin new dialogue
session.beginDialog("/get_password");
}
]);
bot.dialog("/get_password", [
function(session){
//prompt user for password here
botbuilder.Prompts.text(session, "What is your password?");
},
function(session, results){
//store password
session.userData.password = results.response;
//check credentials
if(checkCredentials(session.userData.username, session.userData.password){
//good credentials, send to post-authentication dialog
session.beginDialog("/authenticated");
} else {
//bad credentials
//reset user data and retry
session.userData.username = "";
session.userData.password = "";
session.beginDialog("/get_username");
}
}
]);
You can actually check out a working example of the above Method 2 code here.
方法三:
让网页与嵌入式 WebChat 机器人通信的首选方式是通过Direct Line REST API,它允许您创建“反向通道”。
使用 WebChat 控件 (which you can download and learn about from the repo here),您可以设置嵌入式机器人和包含它的网页,以便通过侦听和广播您定义的事件来相互通信。
You can see a great example of this by checking out this example code that shows the client side.
While this code demonstrates what the bot & server-side code is doing behind the scenes.
然后您可以使用此方法让您的机器人从您的网页侦听身份验证活动,并在用户进行身份验证时广播该活动并附加所需的凭据,以便您的机器人可以使用它们。
我希望这会有所帮助!