MSAL - 重定向 URI 上的 MsalGuard - 进入重定向循环

发布于 2025-01-15 16:31:42 字数 1728 浏览 1 评论 0原文

我正在我的角度应用程序中实现 msal 并使用 msal-v1 库。

我从记录的示例中获取了 angular-7 示例,并尝试在我的企业应用程序中实现以下代码。

我在 app.module.ts 和 app-routing.module.ts 中添加了 popup as false

MsalModule.forRoot(
      {
        auth: {
          clientId: "app client id",
          authority:
            "https://login.microsoftonline.com/tenant.onmicrosoft.com/",
          validateAuthority: true,
          redirectUri: window.location.origin,
          postLogoutRedirectUri:
            window.location.origin,
          navigateToLoginRequestUrl: true,
        },
        cache: {
          cacheLocation: "localStorage",
          storeAuthStateInCookie: isIE, // set to true for IE 11
        },
      },
      {
        popUp: false,
        consentScopes: ["user.read", "openid", "profile"],
        unprotectedResources: ["https://www.microsoft.com/en-us/"],
        protectedResourceMap,
        extraQueryParameters: {},
      }
    )

,我在空路由上添加了 MsalGuard 因为我的应用程序不会有任何登录按钮和默认登陆页面也需要身份验证。

const routes: Routes = [
  {
    path: "",
    component: HomeComponent,
    canActivate: [MsalGuard],
  },
  {
    path: "profile",
    component: ProfileComponent,
    canActivate: [MsalGuard],
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { useHash: false })],
  exports: [RouterModule],
})
export class AppRoutingModule {}

通过这样做,如果我直接点击域 URL(即重定向 URI)而不使用任何路由,我就能够在本地存储中正确授权并获取令牌,但是如果我们清除本地存储然后直接导航到配置文件路径,我就无法获取令牌,因为它正在进入连续的重定向循环

应用程序重定向到/#id_token=eyJ,然后重定向到/profile/#id_token=eyJ,依此类推。

请指导我如何保护我的角度应用程序中的所有路由而不进入重定向循环。

msal version - 1.4.4
msal-angular - 1.1.2

I'm implementing msal in my angular application and I'm using msal-v1 library.

I have taken angular-7 sample from the documented samples and tried implementing the below code in my enterprise application.

I have added popup as false in app.module.ts

MsalModule.forRoot(
      {
        auth: {
          clientId: "app client id",
          authority:
            "https://login.microsoftonline.com/tenant.onmicrosoft.com/",
          validateAuthority: true,
          redirectUri: window.location.origin,
          postLogoutRedirectUri:
            window.location.origin,
          navigateToLoginRequestUrl: true,
        },
        cache: {
          cacheLocation: "localStorage",
          storeAuthStateInCookie: isIE, // set to true for IE 11
        },
      },
      {
        popUp: false,
        consentScopes: ["user.read", "openid", "profile"],
        unprotectedResources: ["https://www.microsoft.com/en-us/"],
        protectedResourceMap,
        extraQueryParameters: {},
      }
    )

and on app-routing.module.ts, I have added MsalGuard on the empty route as my application wont have any login button and the default landing page also needs authentication.

const routes: Routes = [
  {
    path: "",
    component: HomeComponent,
    canActivate: [MsalGuard],
  },
  {
    path: "profile",
    component: ProfileComponent,
    canActivate: [MsalGuard],
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { useHash: false })],
  exports: [RouterModule],
})
export class AppRoutingModule {}

By doing this, if I directly hit the domain URL(i.e. redirect URI) with out any route, I was able to authorize and get token properly in the localstorage, but if we clear local storage and then directly navigate to profile path, I was not able to get the token as it is entering a continuously redirect loop.

the application is redirecting to /#id_token=eyJ and then to /profile/#id_token=eyJ and so on.

please guide me on how to guard all routes in my angular application and not enter the redirect loop.

msal version - 1.4.4
msal-angular - 1.1.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2025-01-22 16:31:42

我在 github(msal-v1) 论坛中讨论了相同的问题,并通过设置不带 MsalGuard 但相同 的新路线找到了替代解决方案Home组件

const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    canActivate: [
      MsalGuard
    ]
  },
  {
    path: 'auth-redirect',
    component: HomeComponent
  },
  {
    path: 'profile',
    component: ProfileComponent,
    canActivate: [
      MsalGuard
    ]
  }
];

此处讨论了完整的详细信息

I have discussed the same issue in the github(msal-v1) forum and have found a alternate solution by setting a new route without the MsalGuard but to the same HomeComponent.

const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    canActivate: [
      MsalGuard
    ]
  },
  {
    path: 'auth-redirect',
    component: HomeComponent
  },
  {
    path: 'profile',
    component: ProfileComponent,
    canActivate: [
      MsalGuard
    ]
  }
];

full details are discussed here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文