简体   繁体   English

在基于多租户,基于天蓝色AD的应用程序中代表用户获取令牌-没有配置访问用户信息的权限?

[英]Obtaining a token on behalf of a user in a multi-tenanted, azure AD based application - No permission to access user information is configured?

I am currently prototyping an authentication model for a series of cloud based micro services and have run into a wall when trying to obtaining a token from Azure AD on behalf of the authenticated user. 我目前正在为一系列基于云的微服务建立身份验证模型的原型,并且在尝试代表已身份验证的用户从Azure AD获取令牌时遇到了麻烦。 I feel I am missing something obvious so I am hoping someone can point me in the right direction. 我觉得我缺少明显的东西,所以我希望有人可以指出正确的方向。

Overview 总览

My prototype consists of two applications: 我的原型包含两个应用程序:

  • An Asp.MVC base UI 一个Asp.MVC基本UI
  • A WebAPI based data service. 基于WebAPI的数据服务。

Both applications are hosted in azure and use Azure's active directory for access management. 这两个应用程序都托管在Azure中,并使用Azure的活动目录进行访问管理。 In azure, I have set up two AD instances: 天蓝色,我设置了两个AD实例:

  • Service Directory: This is the master directory where all applications are registered. 服务目录:这是注册所有应用程序的主目录。
  • Tenant Directory: The AD instance for the tenant. 租户目录:租户的AD实例。 Going forward, there would be one of these per tenant. 展望未来,每个租户将只有其中之一。

Both the UI and data service are registered in the service directory and set to multi-tenant. UI和数据服务都注册在服务目录中,并设置为多租户。 The UI authentication is based on Vibronet's multi-tenant sample and correctly pushes the users through the consent grant flow when they first log in, and then registers the UI application with the users AD instance. UI身份验证基于Vibronet的多租户示例,并在用户首次登录时正确地推动用户通过同意授予流程,然后在用户AD实例中注册UI应用程序。 During the consent grant, the UI requests the following permissions: 在同意授予期间,UI请求以下权限:

  • Access your organization's directory 访问您组织的目录
  • Enable sign-on and read users' profiles 启用登录并读取用户的个人资料
  • Read and write directory data 读写目录数据
  • Read directory data 读取目录数据

The user is then redirected back to the UI application and I can successfully view the users claim information at this point. 然后将用户重定向到UI应用程序,此时我可以成功查看用户的索赔信息。 So, up until this point I believe everything is configured and working correctly. 因此,到现在为止,我相信所有内容均已配置并正常工作。

The Problem 问题

Once the user is authenticated, the UI application should then obtain a token on behalf of the current user to access the back-end data service and this is where the problem lies. 用户通过身份验证后,UI应用程序应代表当前用户获取令牌以访问后端数据服务,这就是问题所在。

The authentication for the data service is based on WebApi-On-Behalf-of sample however whenever I attempt to obtain the token, I get the following error: 数据服务的身份验证基于WebApi-On-Behalf-of示例,但是,每当我尝试获取令牌时,都会出现以下错误:

No permission to access user information is configured for [AppGuid] application, or it is expired or revoked 没有为[AppGuid]应用程序配置访问用户信息的权限,或者该权限已过期或已撤销

Given the permissions granted during the consent flow, however, I believe it should have permissions to access the tenant AD instance and have tried giving the UI application all application and delegated permissions available in AD and re-running the consent flow but still get the same result. 但是,考虑到在同意流程中授予的权限,我认为它应该具有访问租户AD实例的权限,并尝试为UI应用程序提供AD中可用的所有应用程序和委派的权限,并重新运行同意流程,但仍然获得相同的权限结果。

My code for obtaining the on-behalf-of token is as follows: 我用于获取代表令牌的代码如下:

ClientCredential uICredentials = new ClientCredential(StartUp.UiClientId, StartUp.UiSecret);
BootstrapContext bootStrapContext = GetBootstrapContext();
UserAssertion userAssertion = new UserAssertion(bootStrapContext.Token);
AuthenticationContext authContext = new AuthenticationContext(StartUp.adAuthority);
var authResult = authContext.AcquireToken(routerServiceResourceId, uICredentials, userAssertion);

with the exception being raised on the final line. 除了在最后一行提出的例外。 The parameters here are: 这里的参数是:

  • StartUp.UiClientId: The application id for the UI in the service directory. StartUp.UiClientId:服务目录中UI的应用程序ID。
  • StartUp.UiSecret: The secret key for the UI app in the service directory. StartUp.UiSecret:服务目录中UI应用程序的密钥。
  • StartUp.adAuthority: I have tried this with the common AD endpoint for multi-tenant applications ( https://login.microsoftonline.com/common/ ) and with the specific endpoint for this tenant ( https://login.microsoftonline.com/ tenantid ). StartUp.adAuthority:我已经尝试使用多租户应用程序的公共AD终结点( https://login.microsoftonline.com/common/ )和此租户的特定终结点( https://login.microsoftonline.com / tenantid )。 Both give the same result. 两者给出相同的结果。
  • routerServiceResourceId: The App Id URI for the data service. routerServiceResourceId:数据服务的应用程序ID URI。

In addition, I have set 'SaveSigninToken' to true in the TokenValidationParameters for the UI application so I can obtain the BootstrapContext.Token. 另外,我在UI应用程序的TokenValidationParameters中将'SaveSigninToken'设置为true,以便获得BootstrapContext.Token。

As far as I can see, this is everything it should need to work but, as I say, I continually get the error above =/ 据我所知,这就是它应该工作的一切,但是,正如我说的那样,我不断遇到= /以上的错误。

Can anyone suggest a way forward / obvious solution / further reading for this. 任何人都可以为此提出建议/明显的解决方案/进一步阅读。 I seem to be banging my head against this and not getting very far. 我似乎对此不敢相信,也没有走得太远。 I am also not 100% what information is pertinent here regarding the problem so if I have missed any important points, please let me know and I can update the question. 我也不是100%在这里与该问题有关的信息是什么,因此,如果我错过任何要点,请告诉我,我可以更新问题。

The OnBehalf of flow isn't really appropriate in this case. 在这种情况下,按流量的OnBehalf并不真正合适。 The OnBehalf of flow is appropriate when a WebAPI receives an access token and needs to get an access token for a downstream WebAPI. 当WebAPI接收访问令牌并需要获取下游WebAPI的访问令牌时,流程的OnBehalf是合适的。 The flow that is most appropriate in your case is the OpenID Connect code+id_token flow. 在您的情况下最合适的流程是OpenID Connect代码+ id_token流程。 In this flow the WebApp receives an id_token that authenticates the user and an authorization code that allows the WebApp to get an access token for a back end server. 在此流程中,WebApp接收用于对用户进行身份验证的id_token和允许WebApp获取后端服务器访问令牌的授权代码。 The WebApp then redeems the authorization code for the access token. 然后,WebApp赎回访问令牌的授权代码。

The best example of this flow is here: 此流程的最佳示例在这里:

https://github.com/AzureADSamples/WebApp-WebAPI-OpenIDConnect-DotNet https://github.com/AzureADSamples/WebApp-WebAPI-OpenIDConnect-DotNet

Pay attention to Startup.Auth.cs where the AuthorizationCodeRecieved notification is set up. 请注意在其中设置了AuthorizationCodeRecieved通知的Startup.Auth.cs。 This shows how to retrieve the code and redeem it. 这显示了如何检索代码和兑现代码。

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

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