【问题标题】:MSAL - use two different clientid from different directoryMSAL - 使用来自不同目录的两个不同的客户端 ID
【发布时间】:2019-08-09 05:39:38
【问题描述】:

我有一个将被两个不同实体使用的应用程序,每个实体都有自己的 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 中使用多个租户,但我想将其限制为仅两个租户。我应该怎么做?

【问题讨论】:

  • 我写过一篇关于这类应用的文章,希望能给你一些指点:joonasw.net/view/…
  • @juunas,好像是这样,但我没有使用.Net。如果只有 MSAL.js 的文档也能解决这个问题

标签: azure azure-active-directory msal


【解决方案1】:

您可以尝试使用Factory 模式并创建一个方法来为正确的客户端实例化clientApplication。例如:

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);
   }
}

【讨论】:

    【解决方案2】:

    问题我可以在第一个 AAD 中使用多个租户,但我想将其限制为仅两个租户。我应该怎么做? 回答:如果您开发多租户 AD 应用程序,您可以在用户登录后向其颁发者验证“id_token”。例如:

     var msalConfig = {
            auth: {
                clientId: 'b0114608-677e-4eca-ae22-60c32e1782d9', //This is your client ID
                authority: "https://login.microsoftonline.com/common" //This is your tenant info
            },
            cache: {
                cacheLocation: "localStorage",
                storeAuthStateInCookie: true
            }
        };
     var graphConfig = {
            graphMeEndpoint: "https://graph.microsoft.com/v1.0/me"
        };
    
        // create a request object for login or token request calls
        // In scenarios with incremental consent, the request object can be further customized
        var requestObj = {
            scopes: ["user.read"]
        };
    
        var myMSALObj = new Msal.UserAgentApplication(msalConfig);
    
        // Register Callbacks for redirect flow
        // myMSALObj.handleRedirectCallbacks(acquireTokenRedirectCallBack, acquireTokenErrorRedirectCallBack);
        myMSALObj.handleRedirectCallback(authRedirectCallBack);
    
        // difine issuers
        var issuers = new Array();
        issuers[0]="https://login.microsoftonline.com/{TenantId}/v2.0";
        issuers[1]="https://login.microsoftonline.com/{TenantId}/v2.0";
    
        function signIn() {
            myMSALObj.loginPopup(requestObj).then(idToken => {
    
                var issuer =String(idToken.idToken["issuer"])
                console.log(issuer) 
                if(issuers.indexOf(issuer) != -1){
                    //login successfully then your users can do otherthing
    
                }else{
    
                    // your users use a wrong account
                }
    
            }).catch(function (error) {
                //Please check the console for errors
                console.log(error);
            });
        }
    

    更多详情请参考document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-15
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多