【问题标题】:How to support multiple tenants and secret tokens in azure ad scim provisioning如何在 azure ad scim 配置中支持多个租户和秘密令牌
【发布时间】:2020-10-12 18:25:09
【问题描述】:

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

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

但是,这设置为只允许一个租户,并且不使用您在 azure ad 中设置的“秘密令牌”。即使评论明确指出秘密令牌不应留空用于生产。

这是来自参考项目的重要代码

// 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// 其中,tenant_id 是实际租户 ID,TokenAudience 是 8adf8e6e-67b2-4cf2-a259-e3dc5476c621(非画廊应用程序),它可以工作。 但只有当我在天蓝色广告(企业应用程序下的非画廊应用程序)中设置“秘密令牌”时,它才有效。

我已经尝试了各种方法,添加 OnChallenge 告诉我如果我设置了“秘密令牌”就会发送一个挑战,但除此之外我没有进一步了解。

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

更新: 使用 options.TokenValidationParameters.IssuerValidator 我可以验证颁发者,从而使其适用于多个租户。我现在真正无法超越的是,当我在这里输入“秘密令牌”时拨打电话:(见图)

【问题讨论】:

    标签: c# asp.net-core azure-active-directory provisioning scim2


    【解决方案1】:

    所以我发现他们想要的是我生成的那个字段中的 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. Token Audience 我设置为 8adf8e6e-67b2-4cf2-a259-e3dc5476c621,可以在此处阅读 https://docs.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 而是针对您的数据存储。

    【讨论】:

    • 我看到您的令牌在 120 分钟后过期。你是怎么做到的?
    • 我把它改成了 30 年,也就是永久!
    • @RickardLiljeberg 这种方法能否扩展到支持数百甚至数千个 Azure 租户?
    • 我不明白为什么不@Beastwood
    【解决方案2】:

    【讨论】:

    • 这只是一个特定应用程序的设置。和我的问题无关
    • 刚刚检查过,您已经找到了解决问题的方法。我不会进一步更新我的回复。如果您以后需要任何其他信息,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2020-06-28
    • 2019-02-20
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多