我在登录页面中使用 handlewindowcallback()方法,因为当我登录登录页面时,请检查身份验证并重定向到仪表板页面。它工作正常而没有任何缺陷。
但是,当我尝试再次使用AcquireToken检索数据时,应用程序将重定向到登录页面,这仅是第一次发生。这导致内容渲染的延迟。
logincomponent.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');
}
dashboardcomponent.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']
});
});
});
}
WebService.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天了。
这个问题有一些已知的解决方法。您可以通过从adal.config.redurecturi中填充登录名来将用户重定向到重定向URI,而不是登录。
您还可以处理回调组件中的导航,以设置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.
请参阅此类似的线程,看看它是否有助于解决您的问题。