通过检查登录的用户权限提供对 azure blob 的访问?

Provide access to azure blob by checking the logged user rights?

我有一个 ASP.NET 核心应用程序,它在 Azure 中以 blob 形式托管视频和图像。现在,我通过使用在 link 中嵌入过期时间的 SAS 令牌向用户提供 blob 访问权限。问题是 link 可以分发给其他用户,这些用户在 SAS 令牌时间到期并且 link 刷新之前不应访问该 blob。我想以某种方式能够检查正在访问 link 的用户是否已登录应用程序,然后检查它是否有权访问该特定 blob。仅当用户已登录且有权访问 blob 时,才应传送 blob。我的问题与这个类似:link. I have also read something about Azure ABAC 但它似乎是预览版中的一项功能,我不知道它是否可以帮助我。

根据microsoft docs

可以使用您的 Azure AD 帐户或存储帐户访问密钥授权对 Azure 存储的请求。但将 Azure Active Directory (Azure AD) 与 Azure Blob 存储或队列存储结合使用的主要优势是您的凭据不再需要存储在您的代码中。

Instead, you can request an OAuth 2.0 access token from the Microsoft identity platform. Azure AD authenticates the security principal (a user, group, or service principal) running the application. If authentication succeeds, Azure AD returns the access token to the application, and the application can then use the access token to authorize requests to Azure Blob storage or Queue storage.

步骤:

  1. 注册 Azure AD 应用程序

  1. 在 API 权限页面上配置 Azure 应用程序权限,select 添加权限。 在 Microsoft API 选项卡下,select Azure 存储,您可以在委派权限下看到 user_impersonation

接下来,授予管理员同意这些权限

  1. 为用户配置 RABC 角色: Assign an Azure role for access to blob data - Azure Storage | Microsoft Docs :要使用 Azure AD 凭据访问 Azure 门户中的 blob 数据,用户必须具有以下角色分配:
  • 数据访问角色,例如Storage Blob Data ReaderStorage Blob Data Contributor
  • Azure 资源管理器 Reader role,至少了解如何操作 要将这些角色分配给用户,请按照说明进行操作 Assign Azure roles using the Azure portal.
  • 中提供
  1. 然后在用户登录时获取令牌,您可以从中调用 api 访问 azure 存储 blob。 https://<account>.blob.core.windows.net 是给定存储帐户的服务端点。使用此值获取令牌以授权对 Azure blob 存储的请求。将括号中的值替换为您的存储帐户的名称。

通过仅允许登录用户和具有特定访问角色的用户访问,可以确保对 azure blob 的访问安全。

如果需要,您也可以在 how to Secure Azure Blob Storage with Azure API Management & Managed Identities | by Marcus Tee 上查看此博客。

参考文献:

  1. 只允许授权用户访问 Azure 存储中的 Blob URL 容器 - 堆栈溢出
  2. 授权从本机或 Web 访问 blob 或队列数据 应用程序 - Azure 存储 |微软文档
  3. 选择如何授权访问 Azure 门户中的 blob 数据 - Azure 存储 |微软文档