Я реализовал MS-ADAL для угловых 7 и ионных 4, но срок действия токена истекает через каждые 1 час.поэтому, если я использую «acquTokenSilentAsync», чтобы молча получить токен, это дает мне токен, но проблема в том, что я назвал это «acquTokenSilentAsync» от перехватчика, когда inteceptor выбрасывает 401 (Unauthorized), так что API вызывается до того, как токен получен без уведомления, потому что это невозможность перенаправить приложение на домашнюю страницу ..
login() {
debugger
this.showLoading('Loading..').then((loading) => {
loading.present();
let authContext: AuthenticationContext =
this.msAdal.createAuthenticationContext('https://login.windows.net/common');
authContext.tokenCache.readItems().then((cacheToken) => {
debugger
console.log('Token Cache -> ' + cacheToken);
alert(cacheToken);
});
authContext.acquireTokenSilentAsync('https://api/Default/', '343434-af3a-4d88-a1b5-3dac8d6a2a49', '')
.then((authResponse: AuthenticationResult) => {
debugger
loading.dismiss();
console.log('silent ' + authResponse.accessToken);
localStorage.setItem("MS_TOKEN", authResponse.accessToken);
this.homeservice.getUserData();
})
.catch((e: any) => {
debugger
loading.dismiss();
alert('Refresh Error - ' + e);
console.log('Authentication failed', e);
let authContext: AuthenticationContext = this.msAdal.createAuthenticationContext('https://login.windows.net/common');
authContext.acquireTokenAsync('https://api/Default/', '343434-af3a-4d88-a1b5-3dac8d6a2a49', 'https://com.ionic.aopp', '', '')
.then((authResponse: AuthenticationResult) => {
debugger
console.log(authResponse);
localStorage.setItem("MS_TOKEN", authResponse.accessToken);
localStorage.setItem("UserId", authResponse.userInfo.uniqueId.split("@")[0]);
// this.authenticateUser(authResponse.userInfo.uniqueId.split("@")[0]);
this.homeservice.getUserData();
this.authenticationState.next(true);
})
.catch((e: any) => {
debugger
alert('Login error');
console.log('Login error', e)
});
});
});
}