HttpClient 调用受 Azure AD 保护的站点
HttpClient to call Azure AD-protected site
根据一些 Microsoft 示例,我得出了这一点:
ASP.NET 核心设置:
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = Configuration["Authentication:AzureAD:ClientId"],
Authority = Configuration["Authentication:AzureAd:Authority"],
ResponseType = OpenIdConnectResponseType.IdToken,
AutomaticAuthenticate = true,
TokenValidationParameters = new TokenValidationParameters()
});
授权测试端点:
[HttpGet]
[Authorize]
public IActionResult Get()
{
return Ok("SAMPLE TEXT - if you can read this then call it a day :)");
}
客户:
try
{
var result = await authContext.AcquireTokenAsync(WebApiResourceId, WebApiClientId, WebApiRedirectUri, new PlatformParameters(PromptBehavior.Auto));
authorizedClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
var authorizedMessage = await authorizedClient.GetAsync("/AuthorizationTest");
var statusCode = authorizedMessage.StatusCode.ToString();
var message = await authorizedMessage.Content.ReadAsStringAsync();
webBrowser.NavigateToString(message);
}
authorizedClient
初始化为:
private static HttpClientHandler handler = new HttpClientHandler
{
AllowAutoRedirect = true,
CookieContainer = new CookieContainer(),
UseCookies = true
};
private static HttpClient authorizedClient = new HttpClient(handler, false) { BaseAddress = WebApiBaseUri };
我以前只用 BaseAddress 初始化它,后来在 So 上的一个答案之后添加了处理程序。
问题:
即使我正确地从 AAD 获得令牌,WEB API
端点的响应是 HTML(在自动重定向之后),这是 MS 登录页面,错误为“Your browser is set to block cookies.....
”
我应该更改什么以使 HttpClient 正常工作?或者我可以更改 WebApi 配置以不使用 cookies 吗?对于后一种选择,我找不到任何其他选择。
看看这个线程:https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/514 - 它显示了您正在尝试实现的场景。
如评论中所述,您需要使用 Microsoft.AspNetCore.Authentication.JwtBearer
.
包中的 JWT 不记名令牌中间件
Open ID Connect 中间件旨在将用户重定向到登录页面,而不是用于验证访问令牌。可以在此处找到 JWT 不记名令牌中间件的示例用法:https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore/blob/master/TodoListService/Startup.cs.
根据一些 Microsoft 示例,我得出了这一点:
ASP.NET 核心设置:
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = Configuration["Authentication:AzureAD:ClientId"],
Authority = Configuration["Authentication:AzureAd:Authority"],
ResponseType = OpenIdConnectResponseType.IdToken,
AutomaticAuthenticate = true,
TokenValidationParameters = new TokenValidationParameters()
});
授权测试端点:
[HttpGet]
[Authorize]
public IActionResult Get()
{
return Ok("SAMPLE TEXT - if you can read this then call it a day :)");
}
客户:
try
{
var result = await authContext.AcquireTokenAsync(WebApiResourceId, WebApiClientId, WebApiRedirectUri, new PlatformParameters(PromptBehavior.Auto));
authorizedClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
var authorizedMessage = await authorizedClient.GetAsync("/AuthorizationTest");
var statusCode = authorizedMessage.StatusCode.ToString();
var message = await authorizedMessage.Content.ReadAsStringAsync();
webBrowser.NavigateToString(message);
}
authorizedClient
初始化为:
private static HttpClientHandler handler = new HttpClientHandler
{
AllowAutoRedirect = true,
CookieContainer = new CookieContainer(),
UseCookies = true
};
private static HttpClient authorizedClient = new HttpClient(handler, false) { BaseAddress = WebApiBaseUri };
我以前只用 BaseAddress 初始化它,后来在 So 上的一个答案之后添加了处理程序。
问题:
即使我正确地从 AAD 获得令牌,WEB API
端点的响应是 HTML(在自动重定向之后),这是 MS 登录页面,错误为“Your browser is set to block cookies.....
”
我应该更改什么以使 HttpClient 正常工作?或者我可以更改 WebApi 配置以不使用 cookies 吗?对于后一种选择,我找不到任何其他选择。
看看这个线程:https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/514 - 它显示了您正在尝试实现的场景。
如评论中所述,您需要使用 Microsoft.AspNetCore.Authentication.JwtBearer
.
Open ID Connect 中间件旨在将用户重定向到登录页面,而不是用于验证访问令牌。可以在此处找到 JWT 不记名令牌中间件的示例用法:https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore/blob/master/TodoListService/Startup.cs.