首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未通过Microsoft认证的Range11应用程序

未通过Microsoft认证的Range11应用程序
EN

Stack Overflow用户
提问于 2022-06-08 03:47:08
回答 1查看 200关注 0票数 0

我是Azure Active Directory身份验证概念的初学者。

我试图在现有的“角Web核心”应用程序(Multi-tenant).中实现Azure Active身份验证

我已经按照Microsoft Azure Active的指示实现了一切。

但是,在调用"AuthenticationResult“方法之后,"loginRedirect”将在应用程序的角客户端以NULL的形式返回。(这里没有调用特定于应用程序的Web调用)。

问题:单击“登录”按钮的应用程序登录页=>导航到Microsoft Azure Login Page =>,提供凭据,用户将重新定向回应用程序登录页,而不是注册的RedirectURI

提供以下应用程序的结构:

代码语言:javascript
复制
authService => instance of MsalService.
msalBroadCastService => instance of MsalBroadcastService.
msalGuardConfig => instance of MsalGuardConfiguration.
isUserLoggedIn: boolean = false;

  1. app.component.ts

代码语言:javascript
复制
ngOnInit(): void {

this.authService.handleRedirectObservable().subscribe({
      next: (result: AuthenticationResult) => { // few console log code snippet },
      error: (error) => console.log(error)
    });
}

  1. login.component.ts

代码语言:javascript
复制
ngOnInit() {
// have tried the following with "handleRedirectPromise" also, but it didn't work.

this.authService.handleRedirectObservable().subscribe({
            next: (result: AuthenticationResult) => {
                this.msalBroadCastService.inProgress$
                    .pipe(
                        filter((status: InteractionStatus) => status === InteractionStatus.None),
                        takeUntil(this.destroy$)
                    )
                    .subscribe((x) => {
                        // the component "redirect-path" is the redirectURI mentioned in the application configuration file and also in the Azure Active Directory redirectURI section.
                        if (result)
                            this.router.navigate(['redirect-path']);
                    })
            },
            error: (errorMsg) => {
                debugger;
                console.log(errorMsg)
            }
        });
....
....
// Existing logic to load the application "Login" page.
}

onLogin(){
this.authService.instance.handleRedirectPromise()
            .then((tokenResponse) => {
                if (!tokenResponse) {
                    this.isUserLoggedIn = this.authService.instance.getAllAccounts().length == 0;
                    if (this.isUserLoggedIn) {
                        this.authService.loginRedirect({ ...this.msalGuardConfig.authRequest } as RedirectRequest);
                    }
                }
            });
}

  1. app.module.ts

代码语言:javascript
复制
MsalModule.forRoot(new PublicClientApplication(
      {
        auth: {
          clientId: '<Client ID>',
          redirectUri: 'http://localhost:4200/#/redirect-path',
          authority: 'https://login.microsoftonline.com/organizations'
          // authority: 'https://login.microsoftonline.com/common' // Tried this also, but didn't work.
        },
        cache: {
          cacheLocation: 'localStorage',
          storeAuthStateInCookie: isIE
        },
        system: {
          loggerOptions: {
            loggerCallback: (level: LogLevel, message: string, containsPii: boolean): void => {
              if (containsPii) {
                return;
              }
              switch (level) {
                case LogLevel.Error:
                  console.error(message);
                  return;
                case LogLevel.Info:
                  console.info(message);
                  return;
                case LogLevel.Verbose:
                  console.debug(message);
                  return;
                case LogLevel.Warning:
                  console.warn(message);
                  return;
              }
            },
            piiLoggingEnabled: false
          },
          windowHashTimeout: 60000,
          iframeHashTimeout: 6000,
          loadFrameTimeout: 0,
          asyncPopups: false
        }
      }
    ),
      {
        interactionType: InteractionType.Redirect,
        authRequest: {
          scopes: ['user.read']
        }
      },
      {
        interactionType: InteractionType.Redirect,
        protectedResourceMap: new Map(
          [
            ['https://graph.microsoft.com/v1.0/me', ['user.read']]
          ]
        )
      }    
    )

providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: MsalInterceptor,
      multi: true
    }, MsalGuard    
  ]

  1. app-routing.module.ts

代码语言:javascript
复制
const routes: Routes = [
  { path: '', component: LoginComponent, pathMatch: 'full' },
  // { path: '', component: LoginComponent, pathMatch: 'full', canActivate: [MsalGuard] },
  { path: 'redirect-path', component: RedirectPathComponent, pathMatch: 'full' },
  // { path: 'redirect-path', component: RedirectPathComponent, pathMatch: 'full', canActivate: [MsalGuard] },
]

  1. index.html

代码语言:javascript
复制
<body>
  <app-root></app-root>
  <app-redirect></app-redirect>
</body>

如果需要任何其他配置相关信息,请让我知道(我可能漏掉了)。

您的指导将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-06-10 11:13:27

嗨,Debasmita,您不应该在初始化LoginComponent时清除存储在本地存储中的所有数据。这是因为PublicClientApplication被配置为使用本地存储(cacheLocation:'localStorage')

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72539838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档