关于 angular7:如何在运行时更改 msal 权限?

How to change msal authority in runtime?

背景:

我有一个 angular 7 应用。
应用程序使用 Azure Active Directory B2C 和 Msal for angular

进行身份验证

https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-angular

我在 AAD B2C 中创建了 2 个用户流:

  • 签到流程V2
  • 注册流程V2
  • 都自定义了用户界面,

    两者都包含指向其他 AAD B2C 流的重定向按钮。

    (从注册 -> 登录 | 从登录 -> 注册)

    我已经在 angular 中设置了 MSAL 模块,以登录流程作为默认权限
    例如

    1
    2
    3
    4
    5
    MsalModule.forRoot({
    ...,
    authority: <"https://mytenant.b2clogin.com/tfp/mytenant.onmicrosoft.com/B2C_1_Signin">,
    ...
    })

    (注意 B2C_1_Signin 流程名称)

    这运行良好,用户可以登录和注册(在流内的流之间导航并进行身份验证)。

    问题:

    尝试手动更改 MSAL 模块权限时出现问题
    (当应用程序启动时,它在 MSAL init 中定义为 B2C_1_Signin 流)。

    示例:
    用户进入欢迎页面并可以单击登录/注册。
    如果用户点击登录,一切正常,因为权限链接在开始时定义为登录流程(B2C_1_Signin)。

    如果用户想注册,
    我需要更改权限链接
    到选定的 AAD 流 (B2C_1_Signup) 然后我调用 MSAL loginRedirect(),用户重定向到正确的 AAD 流,输入他的详细信息并重定向回应用程序。
    然后应用程序将启动一个新的登录流程并重定向到登录流程,就好像用户未通过身份验证一样。

    这仅在我手动更改权限链接时发生(为了导航到正确的流程,我别无选择)。
    否则,一切都按预期工作。

    如何修改权限,让应用不通过用户认证?

    • 我曾尝试使用没有自定义的默认 AAD 注册 v1 流程。
    • 我试图尊重这个过程以隔离问题(将注册流程设置为默认值,
      手动将权限更改为登录流程,同样的错误(如预期)。
    • 我搜索了许多不同的网站和论坛,但没有一个有相同的问题或修复。

    https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/498

    https://medium.com/@sambowenhughes/configuring-your-angular-6-application-to-use-microsoft-b2c-authentication-99a9ff1403b3

    还有更多……

    用于angular设置的 MSAL:
    "authentication.module.ts"

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    MsalModule.forRoot({

          clientID:"<my-client-id>",

          authority:"https://tenant.b2clogin.com/tfp/tenant.onmicrosoft.com/B2C_1_Signin",

          redirectUri:"http://localhost:4200/dashboard",

          /* default is true */
          validateAuthority: false,

          /* Defaults is 'sessionStorage' */
          cacheLocation :"localStorage",

          /* Defaults is 'redirectUri */      
          postLogoutRedirectUri:"http://localhost:4200/",

          /* Ability to turn off default navigation to start page after login. Default is true. */
          navigateToLoginRequestUrl : false,

          /* Show login popup or redirect. Default:Redirect */
          popUp: false,            
        })

    AuthService package MSAL 身份验证服务:
    "auth.service.ts"

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    constructor(private msalService: MsalService) {}

    public login(scopes: string[] = null, queryParams: string = null): void {    

        this.msalService.authority = this.tenantConfig.baseURL +"/" + this.tenantConfig.tenant +"/" + this.tenantConfig.signInPolicy;

        this.msalService.loginRedirect(scopes, queryParams);
      }

    public signup(scopes: string[] = null, queryParams: string = null): void {    

        this.msalService.authority = this.tenantConfig.baseURL +"/" + this.tenantConfig.tenant +"/" + this.tenantConfig.signUpPolicy;
        this.msalService.loginRedirect(scopes, queryParams);
      }

    我希望 Msal 模块能像往常一样工作,即使我更改了初始定义的权限。


    您是否尝试过针对 MSAL GitHub 库提交问题? MSAL.JS 的问题部分可以在这里找到:https://github.com/AzureAD/microsoft-authentication-library-for-js/issues

    在运行时尝试更改权限实际上存在一个持续的问题:https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/784

    由于它不完全相同,我建议提交一个新的 github 问题。但是,目前看来这不是受支持的功能。


    我们的应用程序也遇到了类似的问题。修复基本上是关闭 MFA。

    如果为登录打开了 MFA,从注册切换到登录不会给我们一个 MFA 提示,并且毫不客气地没有授权用户登录。

    我们的解决方案是检测是否在注册后直接尝试登录,并专门针对该案例使用非 MFA 登录流程。不确定你是否在做 MFA,但如果你是,那值得一看。