通过 REST 创建 B2C 租户 API returns 401/未授权

Creating a B2C Tenant via the REST API returns 401/Unauthorized

我想做什么: 通过 REST API.

创建和配置 B2C 租户

我做了什么:

我在我的默认目录中创建了一个名为 CDTester 的应用程序注册和一个秘密,然后将其添加到具有角色 Contributor 的目标订阅。

我从 user-secrets 加载了一个配置对象 cfg,其中包含 CDTester 的应用程序 ID 和机密、我的默认目录的租户 ID 以及目标订阅的 ID。

在代码中,我使用 ConfidentialClientApplicationBuilder 进行身份验证。

然后我使用我得到的令牌做一些事情:

  1. 我检查特定资源组是否存在
  2. 如果不存在,我就创建
  3. 我检查是否有所需的 B2C 租户名称
  4. 我(尝试)创建租户。

步骤 1-3 工作正常。但是第 4 步给了我 401/Unauthorized。响应正文的格式为内容类型 HTML(而不是 JSON)并且仅包含

You do not have permission to view this directory or page.

因为步骤 1-3 有效,所以我知道我的身份验证是正确的。我什至可以访问特定于 B2C 的 API(我在步骤 3 中使用)。

接下来,我进入门户网站并将 CDTester 设为订阅的所有者,因此应该没有不允许的操作。唉,无济于事,我得到了同样的结果。

作为参考,以下是我的代码的相关部分:

// getting the access token
var app = ConfidentialClientApplicationBuilder
    .Create(authConfig.ApplicationId)
    .WithTenantId(authConfig.LoginTenantId)
    .WithClientSecret(authConfig.ApplicationSecret)
    .Build();
var authResult = await app.AcquireTokenForClient(new[] {"https://management.azure.com/.default"})
                          .ExecuteAsync();

// I use FlurlHttp and set the token
FlurlHttp.Configure(s => s.BeforeCall = c => c.Request.Headers.Add("Authorization", $"Bearer {authResult.AccessToken}"));

// this call works just fine
var nameAvailabilityResponse =  await $"https://management.azure.com/subscriptions/{authConfig.SubscriptionId}/providers/Microsoft.AzureActiveDirectory/checkNameAvailability?api-version=2019-01-01-preview"
                                        .PostJsonAsync(new
                                        {
                                            name = creation.FullDirectoryName,
                                            countryCode = creation.CountryCode
                                        })
                                        .ReceiveJson();
if (!nameAvailabilityResponse.nameAvailable)
{
    await Console.Error.WriteLineAsync(nameAvailabilityResponse.message);
    return ExitCodes.DirectoryNameIsNotAvailable;
}

var displayName = creation.EnvironmentName == "production"
    ? "DEON"
    : $"DEON - {creation.EnvironmentName.ToUpperInvariant()}";


// this one gives the 401
var creationResponse = await $"https://management.azure.com/subscriptions/{authConfig.SubscriptionId}/resourceGroups/{creation.ResourceGroupName}/providers/Microsoft.AzureActiveDirectory/b2cDirectories/{creation.FullDirectoryName}?api-version=2019-01-01-preview"
                        .AllowAnyHttpStatus()
                        .PutJsonAsync(new
                        {
                            location = "Europe",
                            properties = new
                            {
                                createTenantProperties = new
                                {
                                    countryCode = creation.CountryCode,
                                    displayName
                                }
                            },
                            sku = new
                            {
                                name = "Standard",
                                tier = "A0"
                            }
                        });



谷歌搜索带来的相关点击率恰好为零,甚至只是半相关点击率。现在我卡住了。

根据此处提供的答案:https://docs.microsoft.com/en-us/answers/questions/481152/creating-azure-b2c-tenant-via-rest-api-fails-with.html,您似乎无法使用服务主体创建 Azure AD B2C 租户(至少截至今天)。

您需要在实际用户的上下文中执行此请求。换句话说,您需要获取令牌才能在 user_impersonation 范围内执行服务管理 API,这对于服务主体来说是不可能的。