Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Introduzione
Per usare le API di Infrastruttura, come molti altri servizi Microsoft, è prima necessario ottenere un token Microsoft Entra per il servizio Fabric, quindi usare tale token nell'intestazione di autorizzazione della chiamata API.
In questa esercitazione di avvio rapido, si creerà un'app console C#, che acquisirà un token ENTRA ID usando la libreria MSAL.Net, quindi si userà C# HttpClient per chiamare l'API List workspaces.
Creare la registrazione dell'app
Per ottenere un token Microsoft Entra, è prima necessario registrare un'applicazione con Microsoft Entra ID.
Per altre informazioni sulla registrazione di un'applicazione e su proprietà diverse dell'applicazione e su come può essere applicabile allo scenario, vedere Registrare un'app in Microsoft Identity Platform.
In questa esercitazione introduttiva si creerà un client pubblico con URI di reindirizzamento = http://localhost
Accedi al Microsoft Entra admin center come almeno un amministratore di applicazioni cloud.
Passare a Applicazioni > Registrazioni app.
Fare clic su Nuova registrazione.
Immettere un nome di visualizzazione per l'applicazione e aggiungere l'URI di reindirizzamento cliente pubblico
http://localhost
Selezionare Registrazione.
Copiare l'ID applicazione (client) e incollarlo in un Blocco note da usare in un secondo momento.
Ottenere un token
In questa esercitazione si userà MSAL.Net per acquisire un token ENTRA ID per il servizio Fabric con gli ambiti seguenti: Workspace.ReadWrite.All, Item.ReadWrite.All.
Per altre informazioni sull'acquisizione di token con MSAL.Net, vedere Acquisizione di token - Microsoft Authentication Library per .NET.
Incollare l'ID applicazione (client) copiato in precedenza e incollarlo per la variabile ClientId.
Esempio di codice C# per l'acquisizione di un token di accesso Microsoft Entra
#region parameters section
string ClientId = "YourApplicationId";
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
API per le workspace delle liste di chiamate
In questa sezione si vedrà come:
- Creare c# HttpClient con il token acquisito nella sezione precedente.
- Aggiungere
https://api.fabric.microsoft.com/v1/
come URL di base per il client. - Chiamare l'API Elenca aree di lavoro e scrivere la risposta nella console.
Esempio di codice C# per la creazione di un client HTTP e la chiamata dell'API List Workspaces
// 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);
Esempio di codice completo dell'app console C#
using Microsoft.Identity.Client;
using System.Net.Http.Headers;
#region parameters section
string ClientId = "YourApplicationId";
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
#region Create an HTTP client and call the Fabric APIs
// 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);
#endregion