![](/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.