[英]How do I Read/Write configuration data in Azure App Configuration from a Blazor app using the logged in user's credentials?
我正在構建一個門戶網站來管理我們的一些 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 禁止。
“貢獻者”角色不會讓您訪問應用程序配置中的數據。 您需要授予自己應用程序配置數據所有者角色。 更多細節可以在下面的文檔中找到:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.