![](/img/trans.png)
[英]Microsoft Graph API - how to get access token without Authorization Code?
[英]How do I get the access token that was returned through Microsoft.AspNetCore.Components.Authorization
我正在使用 Microsoft.AspNetCore.Components.Authorization 調用我的應用注冊並使用我的憑據登錄。 一切正常,但我不知道如何在登錄后訪問我的訪問令牌。我有一個非常簡單的應用程序。
我按照這里的說明進行操作: 使用 Azure Active Directory 保護 ASP.NET Core Blazor WebAssembly 獨立應用程序
我創建了這個應用程序,它看起來非常像那個頁面上的那個。 但是,我在登錄后找不到任何有關如何獲取令牌的信息。
謝謝你的幫助。
如果您使用的是默認應用程序,則在將請求發送到后端時,令牌會添加到 HTTP 客戶端。 這是在創建 http 客戶端的 program.cs 中完成的:
builder.Services.AddHttpClient("BlazorWasmWithAADAuth.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
// Supply HttpClient instances that include access tokens when making requests to the server project
builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("BlazorWasmWithAADAuth.ServerAPI"));
如果您嘗試獲取用於調用另一個資源的令牌,則必須為該端點創建另一個 http 客戶端,並通過創建自定義授權消息處理程序來添加令牌。
要獲取訪問令牌,您需要注入 IAccessTokenProvider 並請求訪問令牌。
您可以在 razor 組件中嘗試以下操作...
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject IAccessTokenProvider TokenProvider
var tokenResult = await TokenProvider.RequestAccessToken();
if (tokenResult.TryGetToken(out var token))
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.