0

我一直在登录页面中使用handleWindowCallback()方法,因为当我登录登录页面时会检查身份验证并重定向到仪表板页面。它工作正常,没有任何缺陷。

但是当我再次尝试使用 acquireToken 检索数据时,应用程序正在重定向到登录页面,这只是第一次发生。这会导致内容呈现延迟。

登录组件.ts

ngOnInit() {
    this.webService.handleWindowCallback();
    console.log(this.webService.authenticate);
    if (this.webService.authenticate) {
      console.log('Redirecting to dashboard');
      this.router.navigate(['/', 'dashboard']);
    }
    console.log('login loaded');
  }

仪表板组件.ts

ngOnInit() {
    this.showLoader = true;
    this.webService.getUser();
    this.getdata();
    console.log('content loaded');
  }

getdata() {
    const listname = 'Tenets';
    const queryParams = '?$filter=Title eq \'Dashboard\'&$select=Title,Description,Small_x0020_Text,Large_x0020_Text';
    this.webService.getdata(listname, queryParams).subscribe(data => {
      // debugger;
      console.log(data);
      console.log('content loading');
      Object.keys(data['value']).forEach(ele => {
        this.dashboardItems.push({
          'link': data['value'][ele]['Description'],
          'smallText': data['value'][ele]['Small_x0020_Text'],
          'largeText': data['value'][ele]['Large_x0020_Text']
        });
      });
    });
  }

网络服务.ts

getdata(listname, queryParams): Observable<any> {
    const url = environment.config.spUrl + environment.config.spSiteCollection
      + '_api/web/lists/getByTitle(\'' + listname + '\')/items' + queryParams;
    return this.adalService.acquireToken(environment.config.spUrl).flatMap(
      token => {
        const headersParam = new HttpHeaders({
          'Content-Type': 'application/json;odata=verbose',
          'Authorization': 'Bearer ' + token.toString()
        });
        return this.http.get(url, { headers: headersParam });
      });
  }

我需要在 ngOnInit() getdata() 期间检索数据,它应该加载一次,它将呈现内容而无需再次重定向到 loginComponent。

请帮助解决这个问题。它已经吃了我的 2 天。

4

1 回答 1

0

此问题有一些已知的解决方法。您可以通过从 adal.config.reducectURI 填充 loginStartPage 将用户重定向回重定向 URI 而不是 loginStartPage。

您还可以在回调组件中处理导航以设置 ADAL 的令牌和用户信息。

handleCallback(hash: string) {
    if (!this.context.isCallback(hash))
        return;

    var requestInfo = this.context.getRequestInfo(hash);
    if (requestInfo.valid)
        this.context.saveTokenFromHash(requestInfo); //Save the token and user information.  
}

可以使用回调组件中的 context.getCachedUser() 函数检索用户,然后导航到所需的页面。

回调代码:

  this.adalService.handleCallback(window.location.hash);
if (this.adalService.userInfo)
    this.router.navigate(['dashboard']); //Navigate to user dashboard
else
    this.router.navigate(['']); //Navigate to home page.

请参考这个类似的线程,看看它是否有助于解决您的问题。

于 2018-12-31T21:18:53.090 回答