我试图遍历图形API响应中的所有者,但一无所获.在cs文件中,我有如下调用:

var owners = await graphServiceClient.Applications["MyId"].Owners.GetAsync(requestConfiguration =>
{
    requestConfiguration.QueryParameters.Select =
        ["id", "displayName", "givenName"];
});

var owner = (Microsoft.Graph.Models.User)owners.Value[0];

//I can get the Id of said owner
_logger.LogInformation($"Result for users {owners.Value[0].Id}");

//But non of the of the following seem to display anything. It displays the log and the values either empty or null
_logger.LogInformation($"Result for users {owners.Value[0].DisplayName}");
_logger.LogInformation($"Result for owner name {owner.DisplayName}");

在try 访问显示名称字段时,我做错了什么?在开发人员图网站、图形用户界面或其他任何名称中,我可以看到至少有一个应用程序的所有者.只是不理解为什么我不能迭代或访问所述字段.蒂娅.

推荐答案

我有一个名为100的应用程序注册,以下用户为所有者:

enter image description here

在使用服务主体身份验证时,请确保分配Application.Read.AllUser.Read.AllApplication类型的权限.

enter image description here

在我的例子中,我使用下面的示例代码列出了应用程序所有者及其显示名称,并成功地获得了response个,如下所示:

using System;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;

class Program
{
    static async Task Main(string[] args)
    {
        var scopes = new[] { "https://graph.microsoft.com/.default" };

        var clientId = "appId";
        var tenantId = "tenantId";
        var clientSecret = "secret";

        var options = new ClientSecretCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        var clientSecretCredential = new ClientSecretCredential(
            tenantId, clientId, clientSecret, options);

        var graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);

        try
        {
            var owners = await graphServiceClient.Applications["appObjId"].Owners.GetAsync(requestConfiguration =>
            {
                requestConfiguration.QueryParameters.Select = new string[] { "id", "displayName", "givenName" };
            });

            foreach (var owner in owners.Value)
            {
                var user = (User)owner;
                Console.WriteLine($"Id: {user.Id}, Display Name: {user.DisplayName}, Given Name: {user.GivenName}");
            }

        }
        catch (ODataError odataError)
        {
            Console.WriteLine(odataError.Error.Code);
            Console.WriteLine(odataError.Error.Message);
        }
    }
}

Response:

enter image description here

如果您使用委派流,则需要分配Application.Read.AllUser.Read.AllDelegated类型的权限.

Reference:. List owners - Microsoft Graph.

Csharp相关问答推荐

EF Core Fluent API中定义的多对多关系

在命令行中使用时安装,但在单击时不会安装

获取ASP.NET核心身份认证cookie名称

有没有办法把+02:00转换成TimeSpan?""

dotnet集合中内部数组的局部变量副本的用途是什么?'

C#.NET依赖项注入顺序澄清

返回TyedResults.BadRequest<;字符串>;时问题详细信息不起作用

DateTime ToString()未以指定格式打印

有没有类似于扩展元素的合并元组的语法?

该函数不能检测两条曲线的交点

带有可选参数的模拟方法返回意外的不同值,具体取决于可选的默认值

.NET 6:如何防止系统生成的日志(log)?

如何使用用于VS代码的.NET Maui扩展在我的iOS/Android设备或模拟器上进行调试?

VS 2022与VS 2019:如何/为什么创建额外的任务?

在C#中,当输入一个方法/局部函数时,我的IEnumerator被重置/重新创建.为什么?

将C#类导入到PowerShell

如何使用ODP.NET C#设置Oracle会话时间长度限制

最小API定义的Swagger标头参数

在使用xUnit和Mock执行单元测试时,控制器ViewResult返回空的Model集合

我什么时候不应该在Dispose中调用EgSuppressFinalize(This)?