问题描述
我想使用msal为angular生成令牌,我将使用该令牌在另一个api中进行身份验证。我需要msal来获取azure广告令牌,然后执行一些操作,然后手动重定向,但是msal登录后会自动重定向(我不希望这样做),因为我的应用程序希望从api编号为2的令牌获得保护,在我的Angular应用中实施的操作说我没有登录,然后再次将我重定向到msal的登录屏幕(预期是因为API 2中的登录需要花费一些时间,而且我还没有其令牌,所以我会在警卫人员内部进行验证以说出是否是否登录。)
这是我到目前为止尝试过的代码:
this.loginSubscription = this.broadcastService.subscribe('msal:loginSuccess',() => {
this.msalToken = sessionStorage['msal.idtoken'];
this.getPhoto();
this.authUserService.hasTerms().subscribe(
Response => {
const hasTerms:boolean = Response['hasAcceptedTherms'];
if(!hasTerms){
this.router.navigate([''])
this.modal.showTermsAndConditions().subscribe(response => {
const declined:boolean = response['declined'];
if(declined)
{
this.router.navigate(['accessDenied']);
return;
}
this.authUserService.auth().subscribe(next => {
this.router.navigate(['msd-homepage']);
});
});
return;
}
else{
this.authUserService.auth().subscribe(next => {
this.router.navigate(['msd-homepage']);});
}
},error => console.log(error));
});
这是我的模块:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MsalModule } from '@azure/msal-angular';
import { LogLevel } from 'msal';
import { environment } from '../../environments/environment';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { UnauthorizedInterceptor } from './interceptors/unauthorized.interceptor';
import { MsalAuthRoutingModule } from './msal-auth-routing.module';
const protectedResourceMap: Array<[string,string[]]> = [['https://graph.microsoft.com/v1.0/me',['user.read','user.read.all']]];
@NgModule({
declarations: [],imports: [
CommonModule,MsalAuthRoutingModule,MsalModule.forRoot({
clientID: 'http://localhost:4200/',authority: `https://login.microsoftonline.com/${environment.tenantId}`,validateAuthority: true,redirectUri: environment.clientUrl,cacheLocation: 'sessionStorage',postLogoutRedirectUri: 'http://localhost:4200/',navigateToLoginRequestUrl: false,popUp: false,consentScopes: ['user.read','user.read.all','group.read.all'],unprotectedResources: ['https://www.microsoft.com/en-us/'],protectedResourceMap,logger: loggerCallback,correlationId: '1234',level: LogLevel.Info,piiLoggingEnabled: true
})
],providers: [
{
provide: HTTP_INTERCEPTORS,useClass: UnauthorizedInterceptor,multi: true
}
],exports: []
})
export class MsalAuthModule { }
export function loggerCallback(logLevel,message,piiEnabled) {
if (!environment.production) {
console.log('MSAL logging => ' + message);
}
}
这是我的守卫:
import { Injectable } from '@angular/core';
import { CanActivate} from '@angular/router';
import { MsalService } from '@azure/msal-angular';
import { AuthUserService } from '../services/auth-user.service';
@Injectable({
providedIn: 'root'
})
export class AuthUserGuard implements CanActivate {
constructor(private authUserService: AuthUserService,private msalservice:MsalService) { }
public canActivate():boolean {
if(this.authUserService.isLoggedIn())
{
return true;
}
else{
this.msalservice.loginRedirect();
return false;
}
}
}
如果我还能提供其他服务,请告诉我,谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)