需要帮助记录/注销用户在会话后TImeout

时间:2017-05-16 20:04:38

标签: javascript jquery html azure token

我正在尝试在我的项目中实现会话超时功能。当用户空闲而不使用网页时,我希望它发出一条提示消息,询问他们是否要继续,如果用户点击是,我想将其重新登录,因为这有助于重新生成Azure Adal令牌,如果他们这样说"不"我想将它们记录下来。

当我尝试登录时遇到错误,我遇到了一些错误。

这是我的源代码!!

计时器功能。

var idleTime = 0;
$(document).ready(function() {
  //Increment the idle time counter every minute.
  var idleInterval = setInterval(timerIncrement, 60000); // 1 minute

  //Zero the idle timer on mouse movement.
  $(this).mousemove(function(e) {
    idleTime = 0;
  });
  $(this).keypress(function(e) {
    idleTime = 0;
  });
});

function timerIncrement() {
  idleTime = idleTime + 1;
  if (idleTime > 1) { // 20 minutes
    window.confirm("Your Session Will Expire in 2 Minutes. Do you want to continue?");
    //window.location.reload();
    if (confirm == true) {
      AuthenticationContext.prototype.login();
    } else {
      AuthenticationContext.prototype.logOut();
    }
  }
  console.log(idleTime);
}
</script>

我的退出和登录功能

AuthenticationContext.prototype.logOut = function() {
  this.clearCache();
  var tenant = 'common';
  var logout = '';
  this._user = null;
  if (this.config.tenant) {
    tenant = this.config.tenant;
  }

  if (this.config.instance) {
    this.instance = this.config.instance;
  }

  if (this.config.postLogoutRedirectUri) {
    logout = 'post_logout_redirect_uri=' + encodeURIComponent(this.config.postLogoutRedirectUri);
  }

  var urlNavigate = this.instance + tenant + '/oauth2/logout?' + logout;
  this._logstatus('Logout navigate to: ' + urlNavigate);
  this.promptUser(urlNavigate);
};

登录功能

AuthenticationContext.prototype.login = function() {
  // Token is not present and user needs to login
  var expectedState = this._guid();
  this.config.state = expectedState;
  this._idTokenNonce = this._guid();
  this._logstatus('Expected state: ' + expectedState + ' startPage:' + window.location);
  this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST, window.location);
  this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR, '');
  this._saveItem(this.CONSTANTS.STORAGE.STATE_LOGIN, expectedState);
  this._saveItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN, this._idTokenNonce);
  this._saveItem(this.CONSTANTS.STORAGE.FAILED_RENEW, '');
  this._saveItem(this.CONSTANTS.STORAGE.ERROR, '');
  this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION, '');


  var urlNavigate = this._getNavigateUrl('id_token', null) + '&nonce=' + encodeURIComponent(this._idTokenNonce);
  this.frameCallInProgress = false;
  this._loginInProgress = true;
  if (this.config.displayCall) {
    // User defined way of handling the navigation
    this.config.displayCall(urlNavigate);
  } else {
    this.promptUser(urlNavigate);
  }
  // callback from redirected page will receive fragment. It needs to call oauth2Callback
};

我收到清除缓存错误

Uncaught TypeError: Cannot read property 'STORAGE' of undefined
    at Object.AuthenticationContext.clearCache (adal.js:396)
    at Object.AuthenticationContext.logOut (adal.js:440)
    at timerIncrement (localhost/:1208)

缓存代码

AuthenticationContext.prototype.clearCache = function() {
  this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY, '');
  this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY, 0);
  this._saveItem(this.CONSTANTS.STORAGE.FAILED_RENEW, '');
  this._saveItem(this.CONSTANTS.STORAGE.SESSION_STATE, '');
  this._saveItem(this.CONSTANTS.STORAGE.STATE_LOGIN, '');
  this._renewStates = [];
  this._saveItem(this.CONSTANTS.STORAGE.STATE_IDTOKEN, '');
  this._saveItem(this.CONSTANTS.STORAGE.START_PAGE, '');
  this._saveItem(this.CONSTANTS.STORAGE.USERNAME, '');
  this._saveItem(this.CONSTANTS.STORAGE.IDTOKEN, '');
  this._saveItem(this.CONSTANTS.STORAGE.ERROR, '');
  this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION, '');
  var keys = this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS);

  if (!this._isEmpty(keys)) {
    keys = keys.split(this.CONSTANTS.RESOURCE_DELIMETER);
    for (var i = 0; i < keys.length; i++) {
      this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY + keys[i], '');
      this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY + keys[i], 0);
    }
  }
  this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS, '');
};

1 个答案:

答案 0 :(得分:0)

您直接调用该函数而不实际创建单例实例。这意味着从未定义CONSTANTS,因为调用函数时调用它的基础构造函数。请查看SPA JS sample on git hub了解如何使用身份验证上下文。有关如何初始化身份验证上下文,您可以在app.js from the sample

中找到它
    // Enter Global Config Values & Instantiate ADAL AuthenticationContext
    window.config = {
        instance: 'https://login.microsoftonline.com/',
        tenant: '[Enter your tenant here, e.g. contoso.onmicrosoft.com]',
        clientId: '[Enter your client_id here, e.g. g075edef-0efa-453b-997b-de1337c29185]',
        postLogoutRedirectUri: window.location.origin,
        cacheLocation: 'localStorage', // enable this for IE, as sessionStorage does not work for localhost.
    };
    var authContext = new AuthenticationContext(config);
相关问题