如何在BLOB存储中从Azure函数连接到托管站点?
我有一个存储HTML文件的BLOB存储容器。有了公共访问级别,我可以看到HTML,但是想法是将其设置为私人。我想让用户进行身份验证并访问HTML文件。
为此,我创建了一个代码中最相关的部分的Azure函数:
#r "Newtonsoft.Json"
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log, string version, string route)
{
var blobUri = "https://mybloburi.blob.core.windows.net/" + route;
var expiresOn = req.Headers.FirstOrDefault(p => p.Key.Equals("x-ms-token-aad-expires-on",
StringComparison.OrdinalIgnoreCase)).Value.FirstOrDefault();
log.LogInformation($"expires On : {expiresOn}");
log.LogInformation($"blob uri : {blobUri}");
var isTokenExpired = (DateTime.Parse(expiresOn, styles: DateTimeStyles.AdjustToUniversal) - DateTime.UtcNow).TotalMinutes < 5;
var bearerToken = isTokenExpired? await RefreshToken(req, log) : req.Headers.
FirstOrDefault(p => p.Key.Equals("x-ms-token-aad-access-token", StringComparison.OrdinalIgnoreCase)).
Value.FirstOrDefault();
log.LogInformation($"bearer token: {bearerToken}");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
client.DefaultRequestHeaders.Add("Accept", "*/*");
client.DefaultRequestHeaders.Add("x-ms-version", "2017-11-09");
var response = await client.GetAsync(blobUri);
log.LogInformation($"response: {response}");
var contentType = response.Content.Headers.FirstOrDefault(p => p.Key.Equals("Content-Type", StringComparison.OrdinalIgnoreCase));
var byteArray = await response.Content.ReadAsByteArrayAsync();
const string defaultContentType = "application/octet-stream";
return new FileContentResult(byteArray, contentType.Value.Any() ? contentType.Value.First() : defaultContentType);
}
}
在Azure函数的集成部分中,我添加了此配置:
然后我创建了一个应用程序注册。在Azure函数的授权选项下,启用了应用程序服务身份验证。在那里,我添加了一个新的身份提供商作为Microsoft,作为提供商,我添加了创建的应用程序注册。我创建了一个具有访问BLOB存储容器的角色组,并将其应用程序ID设置为“允许的令牌受众”。
测试时,我可以对AAD进行身份验证。但是,HTTP客户端获得函数失败,
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
我不知道其他配置要做什么,而且我似乎在文档中找不到任何可以帮助的东西
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@thomas建议。是的,我们确实有一个静态网站,我们有简化的身份验证。
默认情况下,您将对一系列预配置的提供商进行访问,或者您甚至可以注册自定义提供商。
为了配置静态Web应用程序,用于将其用作API函数以进行角色分配,您需要添加
RoleSsource
属性属性属性到auth,这是API函数的路径。
有关更多信息,您可以检查此 document 。。
As suggested by @Thomas. Yes, we do have a static website where we have a streamlined authentication.
For which you will be having a by default access to a series of pre-configured providers, or you can even register a custom provider.
To configure static web apps for using it as an API function for role assignment you need to add
rolesSource
property toauth
and that is the path of API function.For more information you can check this document.