Я нашел свой ответ на эту проблему после прочтения запросов на промежуточное ПО и HttpClient. Если кто-то найдет себя в одной лодке, вот как я это решил.
- Захват azure токена «Носитель» и сохранение токена на стороне сервера в качестве удостоверения личности пользователя.
- Создайте метод для отправки http-запросов в Web Api.
Примерно так:
ПОЛУЧЕНИЕ И ХРАНЕНИЕ ЖЕТОКА Это происходит в вашем файле startup.cs, где вы настраиваете сервисы для AzureAd / OpenIdConnect.
builder.Services.Configure(configureOptions);
builder.Services.AddSingleton<IConfigureOptions<OpenIdConnectOptions>, ConfigureAzureOptions>();
builder.AddOpenIdConnect(o =>
{
//Additional config snipped
o.Events = new OpenIdConnectEvents
{
OnTokenValidated = async context =>
{
ClaimsIdentity identity = context.Principal.Identity as ClaimsIdentity;
if (identity != null)
{
identity.AddClaim(new Claim("access_token", context.SecurityToken.RawData));
}
System.Diagnostics.Debug.WriteLine(context.SecurityToken.RawData + "\n\n");
}
};
});
СПОСОБ ОТПРАВКИ ВСЕХ HTTP-ЗАПРОСОВ С ЖЕЛЕТОМ АВТОРИЗАЦИИ.
public async Task<IActionResult> AjaxAction(string url)
{
if (User.Claims == null) return null;
System.Security.Claims.Claim claim = User.Claims.SingleOrDefault(s => s.Type == "access_token");
if (claim == null) return null;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", claim.Value);
string url_e = System.Web.HttpUtility.UrlEncode(url);
HttpResponseMessage response = await httpClient.GetAsync(url);
// Here we ask the framework to dispose the response object a the end of the user resquest
HttpContext.Response.RegisterForDispose(response);
return new HttpResponseMessageResult(response);
}