2017-10-12 118 views
0

我正在使用ng2-adal npm lib來生成id_token和access_token。由於id_token和access_token的有效期分別爲30分鐘和60分鐘,因此我試圖想出一種在後臺每20分鐘刷新一次id_token的方法。一種方法可能是:在客戶端刷新id_token:Angular 4

setTimeout(() => { 
    this.adalService.login(); 
}); 

Adal Service登錄方法將重新生成令牌並將其保存到瀏覽器本地存儲。但是,每次令牌重新生成時,它也會刷新應用程序。那麼,我可以以某種方式避免刷新應用程序,只是用用戶的知識在後臺刷新令牌?

回答

1

如果您不想刷新應用程序,則可以按照adal.js library更新id_token的方式在iframe中運行相同的代碼。

AuthenticationContext.prototype._renewIdToken = function (callback) { 
    // use iframe to try refresh token 
    this.info('renewIdToken is called'); 
    var frameHandle = this._addAdalFrame('adalIdTokenFrame'); 
    var expectedState = this._guid() + '|' + this.config.clientId; 
    this._idTokenNonce = this._guid(); 
    this._saveItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN, this._idTokenNonce); 
    this.config.state = expectedState; 
    // renew happens in iframe, so it keeps javascript context 
    window.renewStates.push(expectedState); 

    this.verbose('Renew Idtoken Expected state: ' + expectedState); 
    var urlNavigate = this._getNavigateUrl('id_token', null) + '&prompt=none'; 
    urlNavigate = this._addHintParameters(urlNavigate); 

    urlNavigate += '&nonce=' + encodeURIComponent(this._idTokenNonce); 
    this.registerCallback(expectedState, this.config.clientId, callback); 
    this.idTokenNonce = null; 
    this.verbose('Navigate to:' + urlNavigate); 
    frameHandle.src = 'about:blank'; 
    this._loadFrameTimeout(urlNavigate, 'adalIdTokenFrame', this.config.clientId); 
}; 

然而ng2-adal庫不提供_renewIdToken方法,如果你也想這個功能,你可以從its project site of GitHub提交反饋。

相關問題