首页 > 解决方案 > MSAL - 使用来自不同目录的两个不同的客户端 ID

问题描述

我有一个将由两个不同实体使用的应用程序,每个实体都有自己的 Azure Active Directory。

最初,我使用的代码是:

var msalConfig = {
        auth: {
            clientId: '<client-id-1>'
            authority: "https://login.microsoftonline.com/<tenant-id>" 
        },
        cache: {
            cacheLocation: "localStorage",
            storeAuthStateInCookie: true
        }
    };

现在我想要发生的是,我可以放置两个不同的客户端 ID 和租户 ID 吗?

我可以在第一个 AAD 中使用多个租户,但我想将其限制为只有两个租户。我的方法应该是什么?

标签: azureazure-active-directorymsal

解决方案


You could try using the Factory pattern and create a method that will instantiate the clientApplication for the proper client. For example:

const msalConfigFoo = {
        auth: {
            clientId: '<client-id-1>'
            authority: "https://login.microsoftonline.com/<tenant-id>" 
        },
        cache: {
            cacheLocation: "localStorage",
            storeAuthStateInCookie: true
        }
    };

var msalConfigBar = {
        auth: {
            clientId: '<client-id-2>'
            authority: "https://login.microsoftonline.com/<tenant-id>" 
        },
        cache: {
            cacheLocation: "localStorage",
            storeAuthStateInCookie: true
        }
    };

function getClientApplication(clientType) {
   if (clientType == "foo") {
      return new Msal.UserAgentApplication(msalConfigFoo);
   } else {
      return new Msal.UserAgentApplication(msalConfigBar);
   }
}


推荐阅读