简体   繁体   English

Azure AD - 如何验证下游 API 中的角色

[英]Azure AD - How to validate roles in downstream API

I have a web app that authenticates users and calls a downstream web API as described here .我有一个 Web 应用程序,它对用户进行身份验证并调用下游 Web API,如此所述。 In my case, it's an MVC website, to an AWS Lamba Function.在我的例子中,它是一个 MVC 网站,是一个 AWS Lamba 函数。 Currently, my MVC website has both authorization and authentication but the Lambda Function only has authentication as I'm not sure about the best approach to perform authorization on the backend.目前,我的 MVC 网站同时具有授权和身份验证,但 Lambda 函数仅具有身份验证,因为我不确定在后端执行授权的最佳方法。

I currently see two possible solutions, have the website include the roles in the access token, or have the Lambda Authorizer fetch the roles from Azure AD.我目前看到两种可能的解决方案,让网站在访问令牌中包含角色,或者让 Lambda Authorizer 从 Azure AD 获取角色。 I'm leaning towards the first solution as it seems the simplest, however, I'm not sure how to include the roles in the access token as it currently doesn't have any.我倾向于第一个解决方案,因为它似乎最简单,但是,我不确定如何在访问令牌中包含角色,因为它目前没有任何角色。

Can anybody tell me which approach is recommended, and perhaps included some resources on how to do it.任何人都可以告诉我推荐哪种方法,也许包括一些关于如何做到这一点的资源。

string accessToken;
try
{
    accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(new[] { _lambdaAuthoriserScope });
}
catch (Exception)
{
    Console.WriteLine("Failed to get access token");
    throw;
}
_client.DefaultRequestHeaders.Add("IdToken", accessToken);
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Thanks, Adam谢谢,亚当

In general, claims in an access token is controlled by the resource that will consume that access token.通常,访问令牌中的声明由将使用该访问令牌的资源控制。 In your case the resource is the AWS Lambda Function, that means it dictates what sort of claims it needs in the access tokens that it will accept.在您的情况下,资源是 AWS Lambda 函数,这意味着它在它将接受的访问令牌中规定了它需要什么样的声明。 So you should define these roles in the app registration that you use with the Lambda Function (not sure how you are doing that).因此,您应该在与 Lambda 函数一起使用的应用程序注册中定义这些角色(不确定您是如何做到的)。 This tutorial covers role-based authorization for client-app-calls-web-API scenarios.教程涵盖客户端应用程序调用 Web API 场景的基于角色的授权。

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

相关问题 Azure AD应用程序角色 - Azure AD application roles 如何通过MVC Azure AD身份验证从AD获取角色? - How can I get roles from AD with MVC Azure AD Authentication? 如何获取AccessToken来访问使用Azure AD保护的Web API - how to get AccessToken to access web api secured with Azure AD 将应用程序角色作为自定义声明添加到Azure AD身份 - Add application roles as custom claims to Azure AD identities 具有Azure AD角色的MVC Webapp隐藏/显示网站选项卡 - MVC Webapp hide/show website tabs with Azure AD Roles 天蓝色广告图API无法提取用户信息 - azure ad graph api not pulling user information Azure AD Graph API和WsFederation身份验证 - Azure AD Graph API and WsFederation Authentication 如何将Azure AD身份验证与已自动添加服务引用的Web API服务集成? - How to integrate Azure AD Authentication with a Web API service whose service reference has been added automatically? 如何从HttpContext获取JWT(使用OpenIdConnect)并将其传递给Azure AD Graph API - How to get the JWT (using OpenIdConnect) from HttpContext, and pass to Azure AD Graph API 如何使用Azure AD访问令牌对.NET Core 2.1 API进行身份验证 - How to Authenticate to .NET Core 2.1 API using Azure AD Access Token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM