访问 MS Graph SDK 中的 ApplicationCollectionResponse 中解析的应用程序。

huangapple go评论136阅读模式
英文:

Accessing parsed Applications from ApplicationCollectionResponse in MS Graph SDK

问题

我想提取我的应用程序的应用程序注册和服务主体,只知道应用程序的 appId。这需要我将 (appId='appId') 添加到 URIs。

https://graph.microsoft.com/v1.0/applications(appId='appId')
https://graph.microsoft.com/v1.0/servicePrincipals(appId='appId')

由于无法直接添加到 URI,我不得不使用 RequestInformation,然后使用 RequestAdapter.SendAsync 来获取信息。以下是完整的代码:

var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
requestInfo.URI = new Uri(fixedUri);
var apps = await graphServiceClient.RequestAdapter.SendAsync(requestInfo, ApplicationCollectionResponse.CreateFromDiscriminatorValue).ConfigureAwait(false);

这可以正常工作。但是结果变量的 Value 属性为空,因此我没有获取到解析的 Application。但是我在 apps.AdditionalData 中看到应用程序作为数据字典,所以我知道数据已返回。我需要做什么才能填充 Value

请注意,我已经找到了 graphServiceClient.ApplicationsWithAppIdgraphServiceClient.ServicePrincipalWithAppId,所以我已经有了解决方案,但我仍然对手动方式感到好奇。

英文:

I want to extract the app registration and service principal for my application knowing only the appId. This requires me to append (appId='appId') to the URIs.

https://graph.microsoft.com/v1.0/applications(appId='appId')
https://graph.microsoft.com/v1.0/servicePrincipals(appId='appId')

Since you cannot append to the URI, I had to resort to using RequestInformation, and then using RequestAdapter.SendAsync to fetch the info. Here's the full code

var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
requestInfo.URI = new Uri(fixedUri);
var apps = await graphServiceClient.RequestAdapter.SendAsync(requestInfo, ApplicationCollectionResponse.CreateFromDiscriminatorValue).ConfigureAwait(false);

That works fine. But the result variable has an empty Value property, so I'm not getting any parsed Application back. I do however see the application as data dictionary in apps.AdditionalData so I know the data gets returned. What do I need to do to get the Value populated?

Note that I already found graphServiceClient.ApplicationsWithAppId and graphServiceClient.ServicePrincipalWithAppId, so I do have a solution, but I'm still curious about the manual way

答案1

得分: 0

原因是https://graph.microsoft.com/v1.0/applications(appId='appId')不返回一个集合,而是单个实体,所以您需要使用Application.CreateFromDiscriminatorValue而不是ApplicationCollectionResponse.CreateFromDiscriminatorValue

结果将是Application的一个实例。

var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
requestInfo.URI = new Uri(fixedUri);
var app = await graphServiceClient.RequestAdapter.SendAsync(requestInfo, Application.CreateFromDiscriminatorValue).ConfigureAwait(false);
英文:

The reason is that https://graph.microsoft.com/v1.0/applications(appId='appId') doesn't return a collection but a single entity, so you need to use Application.CreateFromDiscriminatorValue instead of ApplicationCollectionResponse.CreateFromDiscriminatorValue.

The result will be an instance of the Application.

var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
requestInfo.URI = new Uri(fixedUri);
var app = await graphServiceClient.RequestAdapter.SendAsync(requestInfo, Application.CreateFromDiscriminatorValue).ConfigureAwait(false);

答案2

得分: 0

I agree with @user2250152, make use of Application.CreateFromDiscriminatorValue to fetch the Application details.

I used the below code to fetch the application details:

using Microsoft.Graph.Models;
using Microsoft.Graph;
using Azure.Identity;
using Microsoft.Graph.Models.ODataErrors;
using Microsoft.Kiota.Abstractions;

var scopes = new[] { "https://graph.microsoft.com/.default" };

var clientId = "ClientID";
var tenantId = "TenantID";
var clientSecret = "ClientSecret";

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

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

GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestInfo = graphClient.Applications.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(Id='ID')";
requestInfo.URI = new Uri(fixedUri);
var app = await graphClient.RequestAdapter.SendAsync(requestInfo, Application.CreateFromDiscriminatorValue).ConfigureAwait(false);

Console.WriteLine(app.DisplayName);
Console.WriteLine(app.Id);

To fetch the Service Principal details, modify the code like below:

var requestInfo = graphClient.ServicePrincipals.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(Id='SPID')";
requestInfo.URI = new Uri(fixedUri);
var app = await graphClient.RequestAdapter.SendAsync(requestInfo, ServicePrincipal.CreateFromDiscriminatorValue).ConfigureAwait(false);

访问 MS Graph SDK 中的 ApplicationCollectionResponse 中解析的应用程序。

英文:

> I agree with @user2250152, make use of
> Application.CreateFromDiscriminatorValue to fetch the Application details.

I used the below code to fetch the application details:

using Microsoft.Graph.Models;
using Microsoft.Graph;
using Azure.Identity;
using Microsoft.Graph.Models.ODataErrors;
using Microsoft.Kiota.Abstractions;


var scopes = new[] { "https://graph.microsoft.com/.default" };

var clientId = "ClientID";
var tenantId = "TenantID";
var clientSecret = "ClientSecret";

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

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

GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestInfo = graphClient.Applications.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(Id='ID')";
requestInfo.URI = new Uri(fixedUri);
var app = await graphClient.RequestAdapter.SendAsync(requestInfo, Application.CreateFromDiscriminatorValue).ConfigureAwait(false);

Console.WriteLine(app.DisplayName);
Console.WriteLine(app.Id);

访问 MS Graph SDK 中的 ApplicationCollectionResponse 中解析的应用程序。

To fetch the Service Principal details, modify the code like below:

var requestInfo = graphClient.ServicePrincipals.ToGetRequestInformation();
var fixedUri = $"{requestInfo.URI}(Id='SPID')";
requestInfo.URI = new Uri(fixedUri);
var app = await graphClient.RequestAdapter.SendAsync(requestInfo, ServicePrincipal.CreateFromDiscriminatorValue).ConfigureAwait(false);

访问 MS Graph SDK 中的 ApplicationCollectionResponse 中解析的应用程序。

huangapple
  • 本文由 发表于 2023年8月10日 20:08:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定