【发布时间】:2020-11-03 18:10:31
【问题描述】:
我是非常新的 MSAL。所以我只遵循从这里https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/README.md 实施它的基本设置。
我所做的是像这样在 app.module 中设置配置
MsalModule.forRoot({
auth: {
clientId: 'myclientid', // This is your client ID
authority: 'https://login.microsoftonline.com/mytenantid', // This is your tenant ID
redirectUri: 'http://localhost:4200'// This is your redirect URI
},
cache: {
cacheLocation: 'sessionStorage',
storeAuthStateInCookie: isIE, // Set to true for Internet Explorer 11
},
}, {
popUp: !isIE,
consentScopes: [
'user.read',
'openid',
'apiappid/user_impersonation',
],
unprotectedResources: [],
protectedResourceMap: [
[
'https://localhost:44331/',
['apiappid/user_impersonation'],
]
],
extraQueryParameters: {}
})
在路由文件中添加了这个
{path : 'das',canActivate: [MsalGuard], component:CrMainComponent},
这是我的 app.component.ts
import {
Component,
Injectable
} from '@angular/core';
import {
Observable,
Subscription
} from 'rxjs';
import {
BroadcastService,
MsalService
} from '@azure/msal-angular';
import {
CryptoUtils,
Logger,
AuthError,
AuthResponse
} from 'msal';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular-client';
loggedIn: boolean;
public userInfo: any = null;
private subscription: Subscription;
public isIframe: boolean;
constructor(private broadcastService: BroadcastService, private authService: MsalService) {
this.isIframe = window !== window.parent && !window.opener;
if (this.authService.getAccount())
{
//console.info(JSON.stringify(this.authService.getAccount()));
this.loggedIn = true;
} else {
this.loggedIn = false;
//this.login();
}
}
login()
{
const isIE = window.navigator.userAgent.indexOf("MSIE ") > -1 || window.navigator.userAgent.indexOf("Trident/") > -1;
if (isIE) {
this.authService.loginRedirect();
} else {
this.authService.loginPopup();
}
}
logout()
{
this.authService.logout();
}
ngOnInit() {
this.broadcastService.subscribe("msal:loginFailure", (payload) => {
console.log("login failure " + JSON.stringify(payload));
this.loggedIn = false;
});
this.broadcastService.subscribe("msal:loginSuccess", (payload) => {
console.log("login success " + JSON.stringify(payload));
this.loggedIn = true;
//alert("Login Success");
});
this.authService.handleRedirectCallback((redirectError: AuthError, redirectResponse: AuthResponse) => {
if (redirectError) {
console.error("Redirect error: ", redirectError);
return;
}
console.log("Redirect success: ", redirectResponse);
});
}
ngOnDestroy() {
this.broadcastService.getMSALSubject().next(1);
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}
所以我猜,因为我将 Msalguard 指定给我的路由配置,它重定向到 Azure AD 身份验证的微软,并且在成功的身份验证时,它会将我重定向回我的页面。一切正常。
但有时我会遇到错误
Uncaught (in promise): ClientAuthError: Token renewal operation failed due to timeout.
老实说,我不知道我错过了什么或我做错了什么。在我的任何代码中,我都没有对登录过程进行任何操作。当我有这些代码时,这一切都会自动发生。那么我们真的会做些什么来解决这个令牌更新问题吗?我的意思是我们需要手动更新令牌吗?如果有怎么办??
【问题讨论】:
标签: angular jwt azure-active-directory msal