简体   繁体   English

在 .NET 核心 Web ZDB974238714CA8A1434A7CE1D08 中使用 Azure 管理身份限制访问

[英]Restrict Access with Azure Managed Identity in .NET Core Web API

My Azure Web App is calling my Azure API App endpoint.我的 Azure Web 应用程序正在调用我的 Azure ZDB974238714CA8A1434A7CE1D0Z8 端点。 Both app services are in the same Azure subscription and RG.两个应用服务都在同一个 Azure 订阅和 RG 中。 Web App is a .NET Core Web Application and API App is a .NET Core Web API. Web App is a .NET Core Web Application and API App is a .NET Core Web API. Web App is using standard HttpClient class to call the API App endpoint. Web App 正在使用标准 HttpClient class 调用 API App 端点。

I followed https://docs.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=portal%2Cdotnet and created a user-assigned managed identity.我关注了 https://docs.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=portal%2Cdotnet并创建了一个用户分配的托管标识。 Then assigned this identity to the Azure API App with role as contributor.然后将此身份分配给具有贡献者角色的 Azure API 应用程序。

Web App still not assigned with the mentioned managed identity, but it can still access this API App without throwing the expected un-authorized error. Web 应用程序仍未分配上述托管标识,但它仍然可以访问此 API 应用程序而不会引发预期的未经授权的错误。

My question is that how can I restrict that only this managed identity should access the mentioned API app?我的问题是,我该如何限制只有这个托管身份才能访问提到的 API 应用程序? code middlweware or some other Azure settings?代码 middlweware 或其他一些 Azure 设置?

Your API should be receiving a JWT token from your App.您的 API 应该从您的应用程序接收 JWT 令牌。 You need to configure your API to validate the token and reject ones that don't meet your requirements.您需要配置您的 API 以验证令牌并拒绝不符合您要求的令牌。 You get some basic validation out of the box - the token must pass audience, issuer and signing certificate validation - but if you want to restrict it to a single identity then you need to tell it what that is.您可以获得一些开箱即用的基本验证 - 令牌必须通过受众、颁发者和签名证书验证 - 但如果您想将其限制为单个身份,那么您需要告诉它那是什么。

There are a number of ways you can go about this, and Policy based authz is worth a read so you know the the approach ( https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-6.0 ).您可以通过多种方法 go 关于此,基于策略的身份验证值得一读,因此您知道方法( https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies ?view=aspnetcore-6.0 )。

Your identity will have a unique, immutable identifier, the objectId of the managed identity.您的身份将具有唯一的、不可变的标识符,即托管身份的 objectId。 This gets transmitted on the subject claim on your JWT token, so you can lock down your API to that identity with a simple global policy这会在您的 JWT 令牌上的主题声明中传输,因此您可以使用简单的全局策略将您的 API 锁定为该身份

In your API start up where you have an AddAuthorization call, you can add a policy like so在您的 API 启动中有AddAuthorization调用的地方,您可以添加这样的策略

services.AddAuthorization(options =>
{
    options.AddPolicy("OnlyMatchingSubject", policy =>
        policy.RequireAssertion(context => context.User.HasClaim(c =>
            c.Type is ClaimConstants.Sub && c.Value == "your-unique-subject-guid")));

    options.DefaultPolicy = options.GetPolicy("OnlyMatchingSubject");
});

Here, we say that your token must have a sub claim, and it must be a particular value to pass (just get your objectId for your identity from the portal), and then make that the default authentication policy.在这里,我们说您的令牌必须具有子声明,并且必须是要传递的特定值(只需从门户获取您的身份的objectId ),然后将其设为默认身份验证策略。

Here's a full example for a dotnet 6 minimal API.这是 dotnet 6 最小 API 的完整示例。

using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("OnlyMatchingSubject", policy =>
        policy.RequireAssertion(context => context.User.HasClaim(MatchesExpectedSubjectClaim())));

    var policy = options.GetPolicy("OnlyMatchingSubject");
    options.DefaultPolicy = policy;
    options.FallbackPolicy = policy;
});

var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/", () => "Hello World!");

app.Run();

Predicate<Claim> MatchesExpectedSubjectClaim()
{
    return c => c.Type is "sub" && c.Value == "your-unique-subject-guid";
}

You'll need to set an AzureAd config in your settings, as per this documentation https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-app-configuration#config-file您需要根据本文档https: //docs.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-app-configuration 在设置中设置 AzureAd 配置#配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM