如何使用 Microsoft Graph API 在我的 Azure AD B2C 中创建依赖方应用程序?

How do I create a relying party application in my Azure AD B2C using Microsoft Graph API?

我有一个 POST 请求,我正在使用它为我的 Azure AD 租户创建依赖方应用程序。

POST https://graph.microsoft.com/beta/{{tenant_id}}/applications

Headers:

Authorization: {{graph_access_token}}
Content-Type: application/json

Body:

{
    "displayName": "JWT.MS 8",
    "requiredResourceAccess": [
        {
            "resourceAppId": "00000003-0000-0000-c000-000000000000",
            "resourceAccess": [
                {
                    "id": "5b567255-7703-4780-807c-7be8301ae99b",
                    "type": "Role"
                }
            ]
        },
        {
            "resourceAppId": "00000002-0000-0000-c000-000000000000",
            "resourceAccess": [
                {
                    "id": "311a71cc-e848-46a1-bdf8-97ff7156d8e6",
                    "type": "Scope"
                },
                {
                    "id": "6234d376-f627-4f0f-90e0-dff25c5211a3",
                    "type": "Scope"
                }
            ]
        }
    ],
    "signInAudience": "AzureADandPersonalMicrosoftAccount",
    "web": {
        "redirectUris": [
            "https://jwt.ms/"
        ],
        "implicitGrantSettings": {
            "enableIdTokenIssuance": true,
            "enableAccessTokenIssuance": true
        }
    }
}

我收到此应用程序的 201 创建响应。

当我尝试使用隐式流程通过授权端点使用此依赖方时,出现以下错误:

https://{{TenantName}}.b2clogin.com/{{TenantName}}.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1A_TEST_SIGNIN_LATEST&client_id=f7d77147-383f-4da0-9ca4-5da5628574a4&nonce=defaultNonce&redirect_uri=https%3A%2F%2Fjwt.ms%2F&scope=openid&response_type=id_token&prompt=login

AADB2C90018: The client id 'f7d77147-383f-4da0-9ca4-5da5628574a4' specified in the request is not registered in tenant '{{TenantName}}.onmicrosoft.com'.

在 Azure 门户中,我看到未授予 API 权限。

应如何通过图表更改应用程序 API 以便可以将依赖应用程序视为已在 B2C 中注册?

URL请求正确

https://{{TenantName}}.b2clogin.com/{{TenantName}}.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1A_TEST_SIGNIN_LATEST&client_id=f7d77147-383f-4da0-9ca4-5da5628574a4&nonce=defaultNonce&redirect_uri=https%3A%2F%2Fjwt.ms%2F&scope=openid&response_type=id_token&prompt=login

但是应该使用 Azure 门户的 Azure AD B2C blade 创建 Azure AD B2C 应用程序。您需要通过转到 Azure AD B2C 租户并在 Azure 门户上搜索 Azure AD B2C 来注册您的应用程序来注册应用程序,而不是去正常的 Azure AD 应用程序注册 page.Otherwise 它成为故障应用程序然后您收到

AADB2C90018: The client id 'f7d77147-383f-4da0-9ca4-5da5628574a4' specified in the request is not registered in tenant '{{TenantName}}.onmicrosoft.com'.

如果您已在 Azure AD 上注册应用程序,则删除这些应用程序并在 Azure AD B2C 上注册应用程序。

参考:https://docs.microsoft.com/en-us/answers/questions/73443/the-client-id-39039-specified-in-the-request-is-no.html

以下步骤将允许某人通过 Graph API 为 Azure AD B2C 创建依赖方应用程序:

用于检索令牌的应用程序需要以下权限:

POST 申请请求在上面的问题中。

下一步是为该应用程序创建服务主体。

POST https://graph.microsoft.com/beta/{{tenant_id}}/servicePrincipals

Headers:

Authorization: {{graph_access_token}}
Content-Type: application/json

Body

{
  "appId": "{{app_appId}}"
}

其中 {{app_appId}} 是取自 POST /applications

的响应的 appId

下一个请求是授予 oauth2 权限。

POST https://graph.microsoft.com/beta/{{tenant_id}}/oauth2PermissionGrants

Header:

Authorization: {{graph_access_token}}
Content-Type: application/json

Body:

{
    "clientId": "{{service_principal_id}}",
    "consentType": "AllPrincipals",
    "expiryTime": "2030-05-12T00:00:00.9831598Z",
    "principalId": null,
    "resourceId": "{{graph_app_id}}",
    "scope": "openid offline_access"
}

其中 {{service_principal_id}} 是来自 POST /servicePrincipals 响应的 id

其中 {{graph_app_id}} 是显示名称为“Microsoft Graph”的服务主体的 id。这可以通过以下请求检索:

获取 https://graph.microsoft.com/beta/{{tenant_id}}/servicePrincipals?$filter=startswith(displayName,'Microsoft Graph')

Header:

Authorization: {{graph_access_token}}
Content-Type: application/json