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

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

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 来获取信息。以下是完整的代码:

  1. var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
  2. var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
  3. requestInfo.URI = new Uri(fixedUri);
  4. 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

  1. var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
  2. var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
  3. requestInfo.URI = new Uri(fixedUri);
  4. 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的一个实例。

  1. var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
  2. var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
  3. requestInfo.URI = new Uri(fixedUri);
  4. 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.

  1. var requestInfo = graphServiceClient.Applications.ToGetRequestInformation();
  2. var fixedUri = $"{requestInfo.URI}(appId='<my app id here>')";
  3. requestInfo.URI = new Uri(fixedUri);
  4. 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:

  1. using Microsoft.Graph.Models;
  2. using Microsoft.Graph;
  3. using Azure.Identity;
  4. using Microsoft.Graph.Models.ODataErrors;
  5. using Microsoft.Kiota.Abstractions;
  6. var scopes = new[] { "https://graph.microsoft.com/.default" };
  7. var clientId = "ClientID";
  8. var tenantId = "TenantID";
  9. var clientSecret = "ClientSecret";
  10. var options = new ClientSecretCredentialOptions
  11. {
  12. AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
  13. };
  14. var clientSecretCredential = new ClientSecretCredential(
  15. tenantId, clientId, clientSecret, options);
  16. GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential, scopes);
  17. var requestInfo = graphClient.Applications.ToGetRequestInformation();
  18. var fixedUri = $"{requestInfo.URI}(Id='ID')";
  19. requestInfo.URI = new Uri(fixedUri);
  20. var app = await graphClient.RequestAdapter.SendAsync(requestInfo, Application.CreateFromDiscriminatorValue).ConfigureAwait(false);
  21. Console.WriteLine(app.DisplayName);
  22. Console.WriteLine(app.Id);

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

  1. var requestInfo = graphClient.ServicePrincipals.ToGetRequestInformation();
  2. var fixedUri = $"{requestInfo.URI}(Id='SPID')";
  3. requestInfo.URI = new Uri(fixedUri);
  4. 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:

  1. using Microsoft.Graph.Models;
  2. using Microsoft.Graph;
  3. using Azure.Identity;
  4. using Microsoft.Graph.Models.ODataErrors;
  5. using Microsoft.Kiota.Abstractions;
  6. var scopes = new[] { "https://graph.microsoft.com/.default" };
  7. var clientId = "ClientID";
  8. var tenantId = "TenantID";
  9. var clientSecret = "ClientSecret";
  10. var options = new ClientSecretCredentialOptions
  11. {
  12. AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
  13. };
  14. var clientSecretCredential = new ClientSecretCredential(
  15. tenantId, clientId, clientSecret, options);
  16. GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential, scopes);
  17. var requestInfo = graphClient.Applications.ToGetRequestInformation();
  18. var fixedUri = $"{requestInfo.URI}(Id='ID')";
  19. requestInfo.URI = new Uri(fixedUri);
  20. var app = await graphClient.RequestAdapter.SendAsync(requestInfo, Application.CreateFromDiscriminatorValue).ConfigureAwait(false);
  21. Console.WriteLine(app.DisplayName);
  22. Console.WriteLine(app.Id);

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

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

  1. var requestInfo = graphClient.ServicePrincipals.ToGetRequestInformation();
  2. var fixedUri = $"{requestInfo.URI}(Id='SPID')";
  3. requestInfo.URI = new Uri(fixedUri);
  4. 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:

确定