如何仅允许某些特定租户的用户使用Azure AD登录我的应用

时间:2017-01-13 05:31:37

标签: c# angularjs visual-studio azure active-directory

我正在使用AngularJs构建SPA并使用Azure AD进行身份验证。 我的应用是多租户。

但是,我可以看到Azure AD允许来自任何Active Directory的任何租户的用户登录我的应用程序。

他们只是出现了一个同意屏幕(成功登录后),询问他们是否愿意通过应用程序提供对他们信息的访问,如果他们点击是,那么他们是自由重定向到我的应用程序的主页(如果他们有我的应用程序的回调网址)。

我尝试修改我的应用的清单文件,并在knownClientApplications属性中添加了一些租户,以便仅允许来自租户的用户但无济于事。

AngularJs配置代码为:

adalProvider.init(
 {
     instance: AZURE_AD_INSTANCE,
     tenant: 'common',
     clientId: AZURE_AD_CLIENT_ID,
     extraQueryParameter: 'nux=1',
     //cacheLocation: 'localStorage', // enable this for IE, as sessionStorage does not work for localhost.
 }, $httpProvider);

其中,AZURE_AD_INSTANCE和AZURE_AD_CLIENT_ID分别是登录网址和应用的客户端ID。

如果只允许特定租户的用户使用并禁止其他用户,我该怎么办?

1 个答案:

答案 0 :(得分:1)

要仅为多租户申请启用特定租户,我们需要自行验证租户。

以下是您的参考代码(adal-angular.js):

  AdalModule.provider('adalAuthenticationService', function () {
      ...
       var updateDataFromCache = function (resource) {
            // only cache lookup here to not interrupt with events
            var token = _adal.getCachedToken(resource);
            // _oauthData.isAuthenticated = token !== null && token.length > 0;
            _oauthData.isAuthenticated = isAuthenticated(token);
            var user = _adal.getCachedUser() || { userName: '' };
            _oauthData.userName = user.userName;
            _oauthData.profile = user.profile;
            _oauthData.loginError = _adal.getLoginError();
        };


        function isAuthenticated(token) {
            console.log(token);
            if (token !== null && token.length > 0) {
                var decodedToken = _adal._extractIdToken(token);
                var tenantIds = ["04e14a2c-0e9b-42f8-8b22-3c4a2f1d8802", "04e14a2c-0e9b-42f8-8b22-3c4a2f1d8801"];
                var validateTenant = tenantIds.indexOf(decodedToken.tid) !== -1;
                return tenantIds.indexOf(decodedToken.tid) !== -1;
            } else
                return false;                
        }

要让用户在输入错误的租户帐户后启用登录,我们可能会自定义登录方法以附加prompt=login