我正在使用 Micrsoft Graph API 以编程方式注册客户端应用程序、资源服务器,并将角色从资源服务器分配给客户端应用程序。以下是遵循的步骤:
获取访问 token --> https://login.windows.net/
/oauth2/token/?api-version=1.6 客户端应用程序注册 --> https://graph.windows.net/
/applications/?api-version=1.6 客户服务负责人
在 list 中使用以下应用角色注册资源服务器:
"appRoles": [
{
"allowedMemberTypes": [
"Application"
],
"description": "Description of Role - Resource_API_11092017",
"displayName": "Role_Resource_API_11092017",
"id": "5ff0033d-fa87-4a77-9b3d-b4b201dfc32e",
"isEnabled": true,
"value": "Read_Only_Resource_API_11092017"
}
],
资源服务器服务主体创建
为客户端应用分配角色。
当我尝试使用客户端应用程序的凭据获取访问 token 时,我没有获得 JWT 中的任何角色,也没有获得资源服务器的任何信息。
我什至从客户端应用程序中的门户授予资源服务器角色的权限。但还是没有运气。
任何帮助将不胜感激。如果您需要更多详细信息,请告诉我。
谢谢。
@2017 年 11 月 14 日更新:
请找到 JWT 的有效负载。
{
"aud": "https://xxxxx.onmicrosoft.com/Resource_API_11092017",
"iss": "https://sts.windows.net/caa4bd37-xxxx-xxxx-xxxx-bba4bd0e22a6/",
"iat": 1510679930,
"nbf": 1510679930,
"exp": 1510683830,
"aio": "Y2NgYLipff1Ghn65atEd97grbjxxxx==",
"appid": "3ec8690d-xxxx-xxxx-xxxx-739709a758cc",
"appidacr": "1",
"idp": "https://sts.windows.net/caa4bd37-xxxx-xxxx-xxxx-bba4bd0e22a6/",
"tid": "caa4bd37-xxxx-xxxx-xxxx-bba4bd0e22a6",
"ver": "1.0"
}
服务主体中不包含任何角色。但我已使用门户将 Role_Assigned_API_11092017 分配为应用程序权限。此 Role_Assigned_API_11092017 被定义为资源应用 Resource_API_11092017 中的角色。
权限是通过 Portal 从 Resource_API_11092017 授予的。
最佳答案
我能够在 token 中获得角色声明。如果您希望客户端应用程序在资源应用程序中定义角色,则需要执行以下步骤。
- 创建客户端应用。
- 创建客户端应用的服务主体。
- 创建资源应用,其角色定义为:
"appRoles": [{
"allowedMemberTypes": [
"Application"
],
- 为资源应用创建服务主体。
- 使用资源应用中定义的角色修补客户端应用和资源应用。 - 允许客户端应用程序的“所需权限”部分中的角色
- 授予客户端应用程序通过您的管理员帐户从 Azure 门户进行日志记录的权限` 除了第 6 步之外,一切都可以使用 Microsoft Graph API 使用应用程序的访问 token 来完成,该应用程序具有足够的权限在您的目录中注册应用程序。
如果任何人都拥有来自客户端应用程序的授予权限操作的相应 API。请告诉我。目前我们采取手动步骤。
谢谢。
{
"aud": "https://xxxxxxx.onmicrosoft.com/resource_app_11202017",
"iss": "https://sts.windows.net/caa4bd37-xxxx-xxxx-xxxx-bba4bdxxxxa6/",
"iat": 1511209576,
"nbf": 1511209576,
"exp": 1511213476,
"aio": "Y2xxxxBlVE3nxxxxHtx3xSxxxxLAQ==",
"appid": "dffe69ef-xxxx-xxxx-xxxx-3550bxxxxbd7",
"appidacr": "1",
"idp": "https://sts.windows.net/caa4bd37-xxxx-xxxx-xxxx-bba4bdxxxxa6/",
"oid": "f2af0608-xxxx-xxxx-xxxx-a5acacxxxx8d",
"roles": [
"readonly_role_resource_app_11202017",
"writeonly_role_resource_app_11202017"
],
"sub": "f2af0608-xxxx-xxxx-xxxx-a5acacxxxx8d",
"tid": "caa4bd37-xxxx-xxxx-xxxx-bba4bdxxxxa6",
"uti": "VW6inKHUI0SgkuNMi6AcAA",
"ver": "1.0"
}
关于azure - AAD 发行的 JWT token 中缺少角色声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47274580/