提问人:marc_s 提问时间:11/17/2023 更新时间:11/17/2023 访问量:89
无法使用 MS Graph API 从 C# 代码更新 Azure AD 组的显示名称
Failing to update Azure AD group's display name from C# code using MS Graph API
问:
我正在尝试从我的 C# 代码更新 EntraID (Azure AD) 组。displayName
我有一个 Azure 应用注册,该注册具有 (委派的) 权限,并且我已为整个租户应用了管理员同意。MS Graph - Group.ReadWrite.All
使用 ,我可以轻松运行我的 .NET 6.0 控制台应用程序,并使用 ,我可以为当前登录的用户获取该范围的有效访问令牌(交互式 - 使用标准 M365 登录提示对话框)。我还可以查询 MS Graph 以获取我感兴趣的组 - 到目前为止,一切顺利。Microsoft.Identity.Client
PublicClientApplication
Id
但是我的任务的核心 - 更新该 EntraID 组,失败并出现 http 403 - 禁止错误 - 即使我的应用程序注册显然具有权限,并且我获得的访问令牌也列出了该范围(当我在 .displayName
Group.ReadWrite.All
https://jwt.ms
这是更新我的组的显示名称的代码:
public class GroupHandler
{
private HttpClient _client;
public GroupHandler(string graphBaseUrl, string accessToken)
{
_client = new HttpClient();
_client.BaseAddress = new Uri(graphBaseUrl); // this is https://graph.microsoft.com
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}
public async Task UpdateGroupDisplayName(string groupId, string displayName)
{
string requestUrl = $"/v1.0/groups/{groupId}";
PatchObject patchObj = new PatchObject { displayName = displayName };
JsonContent content = JsonContent.Create(patchObj, new MediaTypeHeaderValue("application/json"));
var response = await _client.PatchAsync(requestUrl, content);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Group has been successfully renamed to '{displayName}'");
}
else
{
Console.WriteLine($"ERROR: {response.StatusCode} - {response.ReasonPhrase}");
}
}
}
// this is the class to hold the new display name, to be serialized to JSON for the PATCH call
public class PatchObject
{
public string displayName { get; set; }
}
有什么想法吗?我有点不知所措,因为我的应用程序注册具有必要的权限,他们有管理员同意,令牌也显示了该范围 - 那么为什么它仍然会失败?
答:
我同意@user2250152,登录用户应该有额外的 使用交互式流程时,组管理员等角色以及委派权限。
Group.ReadWrite.All
我的租户中有一个名为 SriGroup
的 Azure AD 组,具有以下属性:
当我尝试通过使用没有目录角色的用户登录并在应用程序中仅授予委派权限来重命名此组时,我也遇到了与以下内容相同的错误:Group.ReadWrite.All
示例代码:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
var scopes = new[] { "Group.ReadWrite.All" };
var tenantId = "tenantId";
var clientId = "appId";
var options = new InteractiveBrowserCredentialOptions
{
TenantId = tenantId,
ClientId = clientId,
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
RedirectUri = new Uri("http://localhost"),
};
var interactiveCredential = new InteractiveBrowserCredential(options);
var graphClient = new GraphServiceClient(interactiveCredential, scopes);
var requestBody = new Group
{
DisplayName = "Sri Group Nov",
};
try
{
await graphClient.Groups["groupId"].PatchAsync(requestBody);
Console.WriteLine($"Group has been successfully renamed");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
throw;
}
响应:
为了解决此错误,我为租户中的该用户分配了组管理员角色,如下所示:
转到 Azure 门户 -> Microsoft Entra ID -> 角色和管理员 -> 所有角色 -> 选择组管理员 -> 添加分配
当我使用具有组管理员角色的用户登录再次运行代码时,我得到了如下响应:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
var scopes = new[] { "Group.ReadWrite.All" };
var tenantId = "tenantId";
var clientId = "appId";
var options = new InteractiveBrowserCredentialOptions
{
TenantId = tenantId,
ClientId = clientId,
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
RedirectUri = new Uri("http://localhost"),
};
var interactiveCredential = new InteractiveBrowserCredential(options);
var graphClient = new GraphServiceClient(interactiveCredential, scopes);
var requestBody = new Group
{
DisplayName = "Sri Group Nov",
};
try
{
await graphClient.Groups["groupId"].PatchAsync(requestBody);
Console.WriteLine($"Group has been successfully renamed");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
throw;
}
响应:
为了确认这一点,我在门户中检查了组名称成功更新的相同内容,如下所示:
或者,您也可以通过授予不需要任何额外角色的应用程序类型的权限来使用客户端凭据流。Group.ReadWrite.All
评论
Group.ReadWrite.All