AZURE AD-自动获取angular5网站的刷新令牌

时间:2018-12-04 19:15:55

标签: azure angular5

我已经连续几天搜索Google,但是找不到与手头问题接近的任何内容。闲置1小时后,我们网站的用户必须重新登录。企业要求静默刷新访问令牌,以便每天不要求用户多次登录。

如何实现?

我们的环境:

AZURE AD-免费版本

网站-angular5和adal-angular5

这是一项服务(已消除了不必要的噪音)

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Adal5Service, Adal5HTTPService } from 'adal-angular5';
import { adal } from 'adal-angular';

const config: adal.Config = {
    tenant: environment.tenant,
    clientId: environment.clientId,
    endpoints: environment.endpoints
}

@Injectable()
export class AuthService {
    constructor(private adalService: Adal5Service,
                private httpAdal: Adal5HTTPService,
                private http: Http) {

      this.adalService.init(config);
      this.adalService.handleWindowCallback();

      if (!this.adalService.userInfo.authenticated) {
        this.adalService.login();
      }
    }

    sendRequest(url: string): Observable<any> {
      const resource = this.adalService.GetResourceForEndpoint(url);
      let authenticatedCall: Observable<any>;

      if (resource) {
        if (this.adalService.userInfo.authenticated) {
          authenticatedCall = 
          this.adalService.acquireToken(resource).flatMap(token => {
            const headers = new Headers();
            headers.append('Authorization', 'Bearer ' + token);

            return this.http.request(url, headers)
          });
        } else {
           this.adalService.login();
        }
     }

     return authenticatedCall;
  }
}

0 个答案:

没有答案