我有一个Azure功能应用程序,一个Azure应用程序服务和一个Azure存储帐户。该函数使用HttpClient向Azure App Service上的ASP.NET MVC操作之一发出GET请求。在App Service和Function App中,我都转到了Azure Portal中的“身份”刀片,并启用了系统身份。我不清楚需要执行哪些其他配置以允许功能应用程序被调用来调用由应用程序服务托管的ASP.NET MVC应用程序中托管的操作。
在ASP.NET Core 3.1 App中,我有一个非常典型的Startup.cs Configure方法:
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
这是我希望功能应用程序向其发出GET请求的控制器动作签名(它生成PDF):
[Authorize]
[Route("/GenerateFile")]
public async Task<IActionResult> GenerateFile(string id, double customerId, string version)
然后在Azure功能应用程序(版本3功能应用程序)中,这是我尝试发出HTTP GET请求的地方。
try
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(reportReviewURL);
_http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// Generates the Final PDF file that is then saved to Azure Storage in the orders container. This is what is served to the customer.
var response = await _http.GetAsync(reportReviewURL + "GenerateFile?version=Final&customerId=" + reportOrder.CustomerId + "&id=" + id);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
log.LogInformation("HttpRequestException thrown: " + ex.Message);
}
我收到的错误消息是:
参数:连接字符串:[未指定连接字符串],资源:https:// MYCUSTOMURL,权限:。异常消息:尝试使用托管服务标识获取令牌。无法获取访问令牌。重试5次后失败。MSI ResponseCode:InternalServerError,响应:{“ exceptionMessage”:“ AADSTS500011:在名为MYAZURETENANT的租户中找不到名为https:// MYCUSTOMURL的资源主体。如果租户的管理员未安装该应用程序,则可能会发生这种情况。 \ r \ n跟踪ID:4f401265-9163-45de-bce9-4744ce633d00 \ r \ n关联ID:3e312f90-3ea6-45a4-87d4- 36416d1b19f0 \ r \ n时间戳:2020-10-12 14:26:00Z“,” errorCode“:” invalid_resource“,”
我当然已经替换了我的应用自定义域和我们的租户ID,以保护隐私。
首先,如果您想使用MSI身份,则不可能。
这是因为MSI不支持公开API。我们需要使用通用的应用程序注册。
1,公开您的网络应用程序的api:
2,将功能aad app添加到应用服务aad app的范围内:
3,然后在函数中更改您的代码,使用azure函数服务主要获取令牌:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句