使用客户端密钥为 Fabric Rest api 生成令牌

问题描述 投票:0回答:1

我正在作为 POC 工作,我必须从 C# 连接到 Fabric Rest API,并且我想创建项目。我可以按照 https://learn.microsoft.com/en-us/rest/api/fabric/articles/get-started/fabric-api-quickstart 上提供的说明创建项目。 我面临的挑战是代币创建。根据此页面上提供的说明,我将获得一个登录页面,并且在身份验证成功后,将生成令牌。 我将在 api 中使用令牌创建调用,但我没有提供身份验证功能。如果可能的话,我需要一些有关如何使用客户端密钥生成令牌的指导。我目前正在使用下面的代码:

using Microsoft.Identity.Client;
#region parameters section 
string ClientId = "clientID";
string Authority = "https://login.microsoftonline.com/organizations";
string RedirectURI = "http://localhost";
#endregion

#region Acquire a token for Fabric APIs 
// In this sample we acquire a token for Fabric service with the scopes  
// Workspace.ReadWrite.All and Item.ReadWrite.All 
string[] scopes = new string[] { "https://api.fabric.microsoft.com/Workspace.ReadWrite.All https://api.fabric.microsoft.com/Item.ReadWrite.All" };

PublicClientApplicationBuilder PublicClientAppBuilder =
        PublicClientApplicationBuilder.Create(ClientId)
        .WithAuthority(Authority)
        .WithRedirectUri(RedirectURI);

IPublicClientApplication PublicClientApplication = PublicClientAppBuilder.Build();

AuthenticationResult result = await PublicClientApplication.AcquireTokenInteractive(scopes)
        .ExecuteAsync()
        .ConfigureAwait(false);

Console.WriteLine(result.AccessToken);
#endregion

我已在 Azure Active Directory 中创建了新的应用程序注册,并添加了 Power Bi 和 azure 存储的 api 权限。我还向管理员提供了对新添加权限的同意。我创建了一个客户端密钥并尝试在邮递员中创建一个令牌。令牌已创建,但在使用此令牌时,它给了我一个错误的请求。当使用从 C# 代码生成的令牌时,它工作正常。

c# azure-active-directory fabric fabric-rest-api
1个回答
0
投票

最初,我注册了一个 Entra ID 应用程序并在其中创建了一个客户端密钥

enter image description here

您需要将上述服务主体添加到 Fabric 工作区,至少提供如下Contributor角色:

enter image description here

在生成令牌之前,请确保通过在管理门户中启用以下选项来允许访问服务主体以使用 Fabric API:

enter image description here

现在,您可以通过 Postman 使用以下参数的客户端凭据流生成访问令牌:

POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
grant_type:client_credentials
client_id:appId
client_secret:secret
scope: https://api.fabric.microsoft.com/.default

回复:

enter image description here

当我使用此令牌通过调用 Fabric API 来列出工作区时,我成功获得了响应,如下所示:

GET https://api.fabric.microsoft.com/v1/workspaces/

回复:

enter image description here

要通过使用客户端凭据流生成令牌来在 C# 中获得相同的响应,您可以使用以下示例代码:

#region
using Microsoft.Identity.Client;
using System.Net.Http.Headers;

string ClientId = "appId";
string ClientSecret = "secret"; 
string Authority = "https://login.microsoftonline.com/tenantId";
#endregion

#region 
string[] scopes = new string[] { "https://api.fabric.microsoft.com/.default" };

ConfidentialClientApplicationBuilder confidentialClientAppBuilder =
    ConfidentialClientApplicationBuilder.Create(ClientId)
    .WithClientSecret(ClientSecret)
    .WithAuthority(Authority);

IConfidentialClientApplication confidentialClientApplication = confidentialClientAppBuilder.Build();

AuthenticationResult result = await confidentialClientApplication.AcquireTokenForClient(scopes)
    .ExecuteAsync()
    .ConfigureAwait(false);

Console.WriteLine(result.AccessToken);
Console.WriteLine();
#endregion


// Create client 
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
string baseUrl = "https://api.fabric.microsoft.com/v1/";
client.BaseAddress = new Uri(baseUrl);

// Call list workspaces API 
HttpResponseMessage response = await client.GetAsync("workspaces");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);

回复:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.