在另一个服务中使用其托管身份对服务调用者进行身份验证

时间:2020-08-21 13:08:00

标签: c# asp.net-web-api azure-active-directory microservices azure-api-management

在另一个App Service / .NET Core WebAPI中验证呼叫者(具有托管身份的App Service / .NET Core WebAPI,而不代表登录用户的身份)的最新技术是什么(还有自己的托管身份)?我想使用Azure API管理隐藏被调用的服务和尽可能少的键。

编辑:

API :应用服务Azure授权/ Active Directory设置:enter image description here API应用注册详细信息:

enter image description here

应用注册/ API应用/身份验证:

  • 隐式授予-仅检查ID令牌

  • 支持的帐户类型-单租户

enter image description here

应用注册/ API应用/公开API:

enter image description here

当我尝试添加客户端应用程序时,出现了以下问题({d1d是我的客户端应用程序服务托管ID 的结尾):

enter image description here

1 个答案:

答案 0 :(得分:3)

您不需要API的托管身份,任何app registration都可以。设置其application id uri并使用Azure.Identity nuget package来获得具有以下内容的令牌:

var managedIdentityCredential = new ManagedIdentityCredential();
var scopes = new []{"{your api application id uri}/.default"}; // E.G: api://3a768fed-640b-420c-8b33-39c701c55199/.default
var accessToken = (await managedIdentityCredential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes))).Token;