![](/img/trans.png)
[英]Azure b2c authorization return 401 (Unauthorized) in Blazor WASM
[英]Creating a B2C Tenant via the REST API returns 401/Unauthorized
我想要做的是:通过REST API创建和配置 B2C 租户。
我做了什么:
我在我的默认目录中创建了一个名为 CDTester 的应用程序注册和一个秘密,然后将它添加到目标订阅中,角色是Contributor
。
我从用户机密加载了一个配置对象 cfg,其中包含 CDTester 的应用程序 ID 和机密,以及我的默认目录的租户 ID 和目标订阅的 ID。
在代码中,我使用ConfidentialClientApplicationBuilder
身份验证。
然后我使用我得到的令牌做一些事情:
步骤 1-3 工作正常。 但是第 4 步给了我一个401/Unauthorized 。 响应的正文被格式化为内容类型的 HTML(而不是 JSON)并且只包含
您无权查看此目录或页面。
因为步骤 1-3 有效,所以我知道我的身份验证是正确的。 我什至可以访问 B2C 特定的 API(我在第 3 步中使用)。
接下来,我进入 Portal 并让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,这对于服务主体是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.