过期后刷新Directline令牌 [英] refresh Directline token after it got expired

查看:57
本文介绍了过期后刷新Directline令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对机器人Webchat应用基于令牌的身份验证,并且聊天仍在继续,但是在令牌过期后面临一个问题.无法连接到机器人.

I'm applying a token based authentication to bot webchat along with the chat being persisted but facing an issue after the token got expired. Unable to connect to bot.

起初,我正在生成令牌并刷新15分钟,然后一切正常.但是,当用户在没有Internet连接的情况下脱机时,假设大约有6到7个小时,由于脱机,刷新令牌发布呼叫不会发生,并且会话存储中将有过期的令牌.比他想和以前进行的相同交谈聊天的时间晚.但是要去FailedToConnect或ExpiredToken问题.由于令牌由于不活动而过期,因此无法再次连接到bot.

At first I'm generating a token and refreshing for 15 mins and every thing working fine till then. But, when user went offline with no internet connectivity, suppose for around 6-7 hours, due to offline the refresh token post call don't happen and there will be an expired token in the session storage. later than he wanted to chat with the same conversation he did before. but was going to FailedToConnect or ExpiredToken issue. As the token got expired due to inactivity unable to connect to bot again.

我的主要目的是如何使用户与之前的会话保持联系.

My main intention is how to connect user with the previous converstion.

谢谢.

 (async function() {
    'use strict';
    const {
            hooks: { usePostActivity },
            hooks: { useDirection },
            ReactWebChat
    } = window.WebChat;
    
     let { token, conversation_Id } = sessionStorage;
    if ( !token ) {
                    const res = await fetch( 'https:/localhost/api/generateToken', { method: 'POST' } );
                    const { token: directLineToken, conversationId: conversationId } = await res.json();
                    sessionStorage[ 'token' ] = directLineToken;
                    sessionStorage[ 'conversation_Id' ] = conversationId;
                    token = directLineToken;
                    conversation_Id = conversationId;
                    }
                    
    if (token) {
        await setInterval(async () => {
        var myHeaders = new Headers();
        myHeaders.append("Authorization","Bearer "+ sessionStorage[ 'token' ]);
        let res = await fetch( 'https://directline.botframework.com/v3/directline/tokens/refresh', {
                                method: 'POST', 
                                headers: myHeaders,
                                });
        const { token: directLineToken, conversationId } = await res.json();
            sessionStorage[ 'token' ] = directLineToken;
            sessionStorage[ 'conversation_Id' ] = conversationId;
            token = directLineToken;
            conversation_Id = conversationId;
        }, 1000*60*15)}
        
                        
    
    
    const  store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
    if(action.payload && action.payload.directLine) {
        const subscription = action.payload.directLine.connectionStatus$.subscribe({
                error: error => console.log( error ),
                next: value => {
                        if ( value === 0 ) {console.log('Uninitialized')} 
                        else if ( value === 1 ) {console.log('Connecting')} 
                        else if ( value === 2 ) {console.log('Online')}
                        else if  ( value === 3 ) {console.log('Expire Token')}
                        else if ( value === 4 ) {console.log('FailedToConnect')}
                        else if ( value === 5 ) {console.log('Ended')}
                }
            });
        }
     if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
        dispatch({
                type: 'WEB_CHAT/SEND_EVENT',
                payload: {
                    name: 'Welcome',
                    value: { language: window.navigator.language }
                    }
                });
            }
            
    
    if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
                action = window.simpleUpdateIn(action, ['payload', 'activity', 'channelData', 'CustomChannel'], () =>"webchat");
                }
    
        return next(action);
    });
    
    
   const  botconnection = createDirectLine( {token,webSockets: true,watermark: "0" });
    
    window.ReactDOM.render(
    <ReactWebChat directLine={botconnection}
                  store={store}
        />,
        document.getElementById('webchat'));
        document.querySelector('#webchat > *').focus();
        })().catch(err => console.error(err));

推荐答案

不幸的是,一旦令牌过期,就无法获取刷新的令牌来继续对话.请求刷新的令牌时,原始令牌仍然必须有效.

Unfortunately, once a token is expired there is no way to obtain a refreshed token in order to continue a conversation. The original token must still be valid when requesting a refreshed token.

关于保持连接,没有什么好选择,但是这里有一个 idea 可能值得尝试:

Regarding maintaining a connection, there's no great option, but here is an idea that may be worth trying:

  • 创建一个服务,该服务在生成令牌时将其与用户的在线状态一起发送到该服务.
  • 该服务将管理令牌刷新,并将刷新后的令牌返回给原始客户端以供使用.
  • 如果用户下线,则返回刷新令牌的调用将失败,该服务会更新用户的在线状态,并遵循何时停止刷新(例如4小时后)的某些条件.
  • 如果用户重新联机,状态将再次更新,刷新的令牌将被使用,并且对话将继续.

希望有帮助!

这篇关于过期后刷新Directline令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆