How do I delete an AppRoleAssignment using the Azure Active Directory .NET SDK?(如何使用 Azure Active Directory .NET SDK 删除 AppRoleAssignment?)
问题描述
我试图弄清楚如何使用 Azure Active Directory 的 Graph API 从组或用户中删除 AppRoleAssignment
.我正在使用 .NET SDK (Microsoft.Azure.ActiveDirectory.GraphClient).
I'm trying to figure out how to delete an AppRoleAssignment
from either an Group or a User using the Graph API for Azure Active Directory. I'm using the .NET SDK (Microsoft.Azure.ActiveDirectory.GraphClient).
我尝试使用每个 IEntityBase
上的标准 DeleteAsync
方法,但它失败并出现错误.它发出一个如下所示的 HTTP 请求:
I've tried using the standard DeleteAsync
method that's on every IEntityBase
, but it fails with an error. It's issuing an HTTP request that looks like this:
DELETE/{tenantId}/directoryObjects/{appRoleAssignment ObjectID}/Microsoft.DirectoryServices.AppRoleAssignment?api-version=1.5
失败并返回 400 Bad Request 并显示错误不支持直接查询此资源类型."
which fails with a 400 Bad Request with the error "Direct queries to this resource type are not supported."
根据 this Microsoft blog post 说您需要执行如下所示的 HTTP 请求:
This isn't the correct way to delete AppRoleAssignments using the Graph API according to this Microsoft blog post which says you need to do an HTTP request that looks like:
DELETE/{tenantId}/users/{user object ID}/appRoleAssignments/{appRoleAs}?api-version=1.5
如果我使用 HttpClient 使用该 URL 格式执行手动 HTTP 请求,它可以工作,但我想知道如何在 .NET 库的范围内执行此操作,而不是自己执行手动 HTTP 请求.
If I do a manual HTTP request using HttpClient using that URL format, it works, but I want to know how to do this within the bounds of the .NET library rather than doing manual HTTP requests myself.
如何通过 .NET 库删除 AppRoleAssignments?
How do I delete AppRoleAssignments via the .NET library?
推荐答案
虽然不固定,但您可以手动发出 HTTP 请求,但仍使用 Azure AD SDK 获取令牌.像这样的:
While it is not fixed, you can make a manual HTTP-request, but still using Azure AD SDK to acqure the token. Something like this:
var tenantId = "<guid> tenant id";
var appId = "<guid> your Azure app id";
var appKey = "your app key";
var authority = "i.e. https://login.windows.net/mycompany.onmicrosoft.com";
var graphUrl = "https://graph.windows.net/";
public async Task RemoveRoleFromUser(Guid userId, string roleObjectId) {
var uri = string.Format("{0}/users/{1}/appRoleAssignments/{2}?api-version=1.5", tenantId, userId, roleObjectId);
await ExecuteRequest<object>(uri, HttpMethod.Delete);
}
private async Task<T> ExecuteRequest<T>(string uri, HttpMethod method = null, Object body = null) where T : class {
if (method == null) method = HttpMethod.Get;
T response;
var token = await AcquireTokenAsyncForApplication();
using (var httpClient = new HttpClient { BaseAddress = getServicePointUri() }) {
var request = new HttpRequestMessage(method, uri);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
if (body != null) {
request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
}
var responseMessage = await httpClient.SendAsync(request).ConfigureAwait(false);
responseMessage.EnsureSuccessStatusCode();
response = await responseMessage.Content.ReadAsAsync<T>();
}
return response;
}
private async Task<string> AcquireTokenAsyncForApplication() {
ClientCredential clientCred = new ClientCredential(appId, appKey);
var authenticationContext = new AuthenticationContext(authority, false);
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(graphUrl, clientCred);
return authenticationResult.AccessToken;
}
private Uri getServicePointUri() {
Uri servicePointUri = new Uri(graphUrl);
Uri serviceRoot = new Uri(servicePointUri, tenantId);
return serviceRoot;
}
这篇关于如何使用 Azure Active Directory .NET SDK 删除 AppRoleAssignment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Azure Active Directory .NET SDK 删除 AppRoleAs


基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01