如何在 azure ad scim 配置中支持多个租户和秘密令牌

2024-01-03

我正在尝试为我们的 Saas 产品创建 Azure AD 配置(使用 scim2)。 我希望多个客户能够连接到他们的 Azure AD 租户。

微软在这里有参考代码:https://github.com/AzureAD/SCIMReferenceCode https://github.com/AzureAD/SCIMReferenceCode

但是,设置为仅允许一个租户,并且不使用您在天蓝色广告中设置的“秘密令牌”。即使评论明确指出秘密令牌不应留空以供生产。

这是参考项目中的重要代码

// Leave the optional Secret Token field blank
            // Azure AD includes an OAuth bearer token issued from Azure AD with each request
            // The following code validates the Azure AD-issued token
            // NOTE: It's not recommended to leave this field blank and rely on a token generated by Azure AD. 
            //       This option is primarily available for testing purposes.
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Authority = this.configuration["Token:TokenIssuer"];
                options.Audience = this.configuration["Token:TokenAudience"];
                options.Events = new JwtBearerEvents
                {
                    OnTokenValidated = context =>
                    {
                        // NOTE: You can optionally take action when the OAuth 2.0 bearer token was validated.

                        return Task.CompletedTask;
                    },
                    OnAuthenticationFailed = AuthenticationFailed
                };
            });

使用该代码,假设 Token:TokenIssuer 设置为https://sts.windows.net/ https://sts.windows.net// 其中,tenant_id 是实际租户 ID,TokenAudience 是 8adf8e6e-67b2-4cf2-a259-e3dc5476c621(非库应用程序)。 但只有当我在天蓝色广告(企业应用程序下的非图库应用程序)中设置“秘密令牌”时将其留空时,它才有效。

我已经尝试了各种各样的事情,添加 OnChallenge 告诉我,如果我设置“秘密令牌”,则会发送一个挑战,但除此之外,我没有取得进一步的进展。

这里处理多个租户和秘密令牌的任何示例代码都会很棒

Update: Using options.TokenValidationParameters.IssuerValidator I can validate the issuer and thus make that work with multiple tenants. What I really can't get past right now is making a call work when I enter a "Secret token" here: (see picture) enter image description here


所以我发现他们想要的是我生成的该字段中的 JWT 令牌。

所以首先我创建了一个生成网络令牌的方法

private string GenerateJSONWebToken()
    {
        // Create token key
        SymmetricSecurityKey securityKey =
            new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Token:TokenSigningKey"]));

        SigningCredentials credentials =
            new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

        // Set token expiration
        DateTime startTime = DateTime.UtcNow;
        DateTime expiryTime = startTime.AddMinutes(120);
        
        // Generate the token
        JwtSecurityToken token =
            new JwtSecurityToken(
                configuration["Token:TokenIssuer"],
                configuration["Token:TokenAudience"],
                null,
                notBefore: startTime,
                expires: expiryTime,
                signingCredentials: credentials);

        string result = new JwtSecurityTokenHandler().WriteToken(token);
        return result;
    }

在我的 appsettings.json 中我添加了

{
"Logging": {
    ...
},
"Token": {
    "TokenAudience": "xxx-xxx-xxx-xxx",
    "TokenIssuer": "https://sts.windows.net/yyyy-yyyy-yyyy/",
    "TokenSigningKey": "zzz"
}
}
  1. 令牌受众我设置为 8adf8e6e-67b2-4cf2-a259-e3dc5476c621 ,可以在此处阅读https://learn.microsoft.com/en-us/azure/active-directory/app-provisioning/use-scim-to-provision-users-and-groups https://learn.microsoft.com/en-us/azure/active-directory/app-provisioning/use-scim-to-provision-users-and-groups.

TL;DR令牌的受众将是库中应用程序的应用程序模板 ID,所有自定义应用程序的应用程序模板 ID 为 8adf8e6e-67b2-4cf2-a259-e3dc5476c621

  1. TokenIssuer 的 yyyy 部分是 azure 广告租户的租户 ID
  2. 签名密钥中的 zzz 只是您选择的密钥。

现在我终于生成了一个包含 appsettings.json 中的值的令牌。 然后,我将此密钥粘贴到 Azure AD 中的“秘密令牌”字段中。

最后,如何制作这个多租户(我的下一步)

  1. 从 appsettings.json 中删除 Token:TokenIssuer
  2. 当您调用GenerateJSONWebToken时,发送客户端Azure AD租户ID并使用它而不是appsettings.json中的静态值(您的客户端将给您这个,或者您通过将应用程序连接到他们而获得它)
  3. 在startup.cs中通知我已经实现了IssuerValidator。更新此内容以验证您的数据存储而不是 appsettings.json。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 azure ad scim 配置中支持多个租户和秘密令牌 的相关文章

随机推荐