Azure OAuth:无法在管理员同意权限的情况下以编程方式创建应用程序
Azure OAuth: Unable to programmatically create app with admin consent for permissions
我正在尝试为 OAuth 创建一个 Azure AD 应用程序并尝试为其分配权限。应用创建成功,但我没有看到正确分配的权限。
以下是我创建应用程序所遵循的步骤。
使用设备代码流获得了访问令牌。 (https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code)
使用以下 C# 代码创建了应用程序
public async Task CallPostWebApiAndProcessResultAsync(string webApiUrl, string accessToken, string body, Action<JObject> processResult)
{
if (!string.IsNullOrEmpty(accessToken))
{
var defaultRequetHeaders = HttpClient.DefaultRequestHeaders;
if (defaultRequetHeaders.Accept == null || !defaultRequetHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
defaultRequetHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
HttpContent postContent = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response = await HttpClient.PostAsync(webApiUrl, postContent);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
JObject result = JsonConvert.DeserializeObject(json) as JObject;
processResult(result);
}
else
{
// Error
}
}
}
上述函数的参数如下:
webAPIUrl
是:“https://graph.microsoft.com/v1.0/applications”
body
是:"{ \"displayName\": \"App with permissions in tenant\", \"requiredResourceAccess\": [ { \"resourceAppId\": \"00000003-0000-0000-c000-000000000000\", \"resourceAccess\": [ { \"id\": \"75359482-378d-4052-8f01-80520e7db3cd\", \"type\": \"Role\" }, { \"id\": \"62a82d76-70ea-41e2-9197-370581804d09\", \"type\": \"Role\" } ]}]}"
我可以验证该应用是否已成功创建,但我无法看到已成功分配给它的权限。
我正在尝试分配 Graph API 权限 Files.ReadWrite.All 和 Group.ReadWrite.All。这些权限也正在添加,但它们的状态是“无法确定状态”。不会自动授予管理员同意。
在获取设备代码时,我使用租户管理员凭据登录,所以我不确定这里还需要什么。
遗憾的是,在网上找不到太多相关信息。如果有人对此有任何想法,请告诉我。
enter image description here
我认为授予管理员同意是一个需要执行的单独步骤:https://docs.microsoft.com/en-us/azure/active-directory/manage-apps/grant-admin-consent#construct-the-url-for-granting-tenant-wide-admin-consent
设置 requiredResourceAccess 仅用于声明您的应用 请求 的权限(或 requires,取决于流量)。这与实际授予这些权限不同。来自 application 对象的 requiredResourceAccess 属性 的文档:
requiredResourceAccess collection
Specifies resources that this application requires access to and the set of OAuth permission scopes and application roles that it needs under each of those resources. This pre-configuration of required resource access drives the consent experience. Not nullable.
授予应用程序请求的权限的一种方法是使用交互式 tenant-wide 同意 URL 获得授予权限的提示,如 Grant tenant-wide admin consent to an application 中所述。
或者,您可以使用 Microsoft Graph 授予这些权限。
应用程序权限是应用程序角色。要授予应用程序权限,您必须在 API 的服务主体的 appRoleAssignedTo 集合中 create an app role assignment:
- 确保存在客户端应用程序的服务主体。如果没有(例如,因为您刚刚创建了应用程序),则 create the service principal。设
{principal-id}
为客户端应用的 servicePrincipal 对象的 id。
- 检索资源应用程序的服务主体(例如,在本例中为 API)。设
{resource-id}
为资源应用的 servicePrincipal 对象的 id。
- 查找您要授予的应用角色的 id。在这种情况下,您已经拥有它(您在应用程序对象的 requiredResourceAccess 中使用了它),但您也可以在 appRoles 集合中查找它资源应用程序的 servicePrincipal 对象。设
{app-role-id}
为您要授予的 appRole 的 id。
- 创建应用角色分配:
POST https://graph.microsoft.com/v1.0/servicePrincipals/{resource-id}/appRoleAssignedTo
{
"principalId": "{principal-id}",
"resourceId": "{resource-id}",
"appRoleId": "{app-role-id}"
}
- 为 API 的每个应用程序角色(应用程序权限)重复步骤 4。
与其手动构建所有这些请求,我建议使用 Microsoft Graph SDK。
警告
通过向 Microsoft Graph 授予应用程序应用程序权限,你可能会授予该应用程序对你的组织和组织数据的非常特权的访问权限。在执行此操作之前,请确保您完全理解其中的含义。
我正在尝试为 OAuth 创建一个 Azure AD 应用程序并尝试为其分配权限。应用创建成功,但我没有看到正确分配的权限。
以下是我创建应用程序所遵循的步骤。
使用设备代码流获得了访问令牌。 (https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code)
使用以下 C# 代码创建了应用程序
public async Task CallPostWebApiAndProcessResultAsync(string webApiUrl, string accessToken, string body, Action<JObject> processResult) { if (!string.IsNullOrEmpty(accessToken)) { var defaultRequetHeaders = HttpClient.DefaultRequestHeaders; if (defaultRequetHeaders.Accept == null || !defaultRequetHeaders.Accept.Any(m => m.MediaType == "application/json")) { HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } defaultRequetHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken); HttpContent postContent = new StringContent(body, Encoding.UTF8, "application/json"); HttpResponseMessage response = await HttpClient.PostAsync(webApiUrl, postContent); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); JObject result = JsonConvert.DeserializeObject(json) as JObject; processResult(result); } else { // Error } } }
上述函数的参数如下:
webAPIUrl
是:“https://graph.microsoft.com/v1.0/applications”body
是:"{ \"displayName\": \"App with permissions in tenant\", \"requiredResourceAccess\": [ { \"resourceAppId\": \"00000003-0000-0000-c000-000000000000\", \"resourceAccess\": [ { \"id\": \"75359482-378d-4052-8f01-80520e7db3cd\", \"type\": \"Role\" }, { \"id\": \"62a82d76-70ea-41e2-9197-370581804d09\", \"type\": \"Role\" } ]}]}"
我可以验证该应用是否已成功创建,但我无法看到已成功分配给它的权限。
我正在尝试分配 Graph API 权限 Files.ReadWrite.All 和 Group.ReadWrite.All。这些权限也正在添加,但它们的状态是“无法确定状态”。不会自动授予管理员同意。
在获取设备代码时,我使用租户管理员凭据登录,所以我不确定这里还需要什么。
遗憾的是,在网上找不到太多相关信息。如果有人对此有任何想法,请告诉我。
enter image description here
我认为授予管理员同意是一个需要执行的单独步骤:https://docs.microsoft.com/en-us/azure/active-directory/manage-apps/grant-admin-consent#construct-the-url-for-granting-tenant-wide-admin-consent
设置 requiredResourceAccess 仅用于声明您的应用 请求 的权限(或 requires,取决于流量)。这与实际授予这些权限不同。来自 application 对象的 requiredResourceAccess 属性 的文档:
requiredResourceAccess collection
Specifies resources that this application requires access to and the set of OAuth permission scopes and application roles that it needs under each of those resources. This pre-configuration of required resource access drives the consent experience. Not nullable.
授予应用程序请求的权限的一种方法是使用交互式 tenant-wide 同意 URL 获得授予权限的提示,如 Grant tenant-wide admin consent to an application 中所述。
或者,您可以使用 Microsoft Graph 授予这些权限。
应用程序权限是应用程序角色。要授予应用程序权限,您必须在 API 的服务主体的 appRoleAssignedTo 集合中 create an app role assignment:
- 确保存在客户端应用程序的服务主体。如果没有(例如,因为您刚刚创建了应用程序),则 create the service principal。设
{principal-id}
为客户端应用的 servicePrincipal 对象的 id。 - 检索资源应用程序的服务主体(例如,在本例中为 API)。设
{resource-id}
为资源应用的 servicePrincipal 对象的 id。 - 查找您要授予的应用角色的 id。在这种情况下,您已经拥有它(您在应用程序对象的 requiredResourceAccess 中使用了它),但您也可以在 appRoles 集合中查找它资源应用程序的 servicePrincipal 对象。设
{app-role-id}
为您要授予的 appRole 的 id。 - 创建应用角色分配:
POST https://graph.microsoft.com/v1.0/servicePrincipals/{resource-id}/appRoleAssignedTo { "principalId": "{principal-id}", "resourceId": "{resource-id}", "appRoleId": "{app-role-id}" }
- 为 API 的每个应用程序角色(应用程序权限)重复步骤 4。
与其手动构建所有这些请求,我建议使用 Microsoft Graph SDK。
警告
通过向 Microsoft Graph 授予应用程序应用程序权限,你可能会授予该应用程序对你的组织和组织数据的非常特权的访问权限。在执行此操作之前,请确保您完全理解其中的含义。