我正在構建一個門戶網站來管理我們的一些 Azure 應用程式配置存盤中的值,以便為一些復雜的 json 配置值提供一個用戶友好的編輯器。
我想我已經完成了所有需要的設定,但在實際使用客戶端時只收到 401 或 403 回應。這個用例的檔案似乎缺乏,所以我希望有人可以就如何實作它提供一些指導。
一些相關的代碼片段:
編輯器.razor
@using Microsoft.Identity.Web
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler
@inject ConfigurationService ConfigurationService
<editor/>
@code{
private object Value { get; set; }
protected override async Task OnInitializedAsync()
{
try
{
Value = await ConfigurationService.GetSettings();
}
catch (Exception ex)
{
ConsentHandler.HandleException(ex);
}
await base.OnInitializedAsync();
}
配置服務.cs
using Azure.Data.AppConfiguration;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Web;
private readonly ConfigurationClient _client;
public ConfigurationClient(IOptions<ConfigurationSettings> options, ITokenAcquisition tokenAcquisition)
{
_client = new ConfigurationClient(new Uri(options.Value.Endpoint), new TokenAcquisitionTokenCredential(tokenAcquisition));
}
public async Task<object> GetSettings()
{
return await ConfigurationClient.GetConfigurationSettingAsync("key");
}
程式.cs
//code
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(new []{"User.Read"})
.AddInMemoryTokenCaches();
builder.Services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor()
.AddMicrosoftIdentityConsentHandler();
//more code
應用設定.json
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "redacted.onmicrosoft.com",
"TenantId": "redacted",
"ClientId": "redacted",
"ClientSecret": "redacted",
"CallbackPath": "/signin-oidc"
}
}
Web 應用已在 Azure AD 中注冊,并設定了身份驗證并正常運行。我創建了一個客戶端密碼,并在應用程式配置中使用它。未設定令牌配置。API 權限如下: Azure AD API 權限 “公開 API”選項卡下未定義任何設定。
我正在測驗的用戶是我自己的,并且在 Azure 應用程式配置資源上具有“貢獻者”角色。
該網站允許登錄,并在訪問相關頁面時正確請求用戶訪問應用程式配置資源的權限。檢查流量顯示應用程式請求配置資源上具有 .default 范圍的訪問令牌,并成功接收到一個。使用令牌會導致 403 禁止。
uj5u.com熱心網友回復:
“貢獻者”角色不會讓您訪問應用程式配置中的資料。您需要授予自己應用程式配置資料所有者角色。更多細節可以在下面的檔案中找到:
https://docs.microsoft.com/en-us/azure/azure-app-configuration/concept-enable-rbac#azure-built-in-roles-for-azure-app-configuration
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/492655.html
