Een desktop-app die web-API's aanroept: codeconfiguratie

Nu u de toepassing hebt gemaakt, leert u hoe u de code configureert met de coördinaten van de toepassing.

Microsoft-bibliotheken die desktop-apps ondersteunen

De volgende Microsoft-bibliotheken ondersteunen desktop-apps:

Taal/framework Project over
GitHub
Pakket Aan de
slag
Gebruikers aanmelden Toegang krijgen tot web-API's Algemeen beschikbaar (GA) of
Openbare preview1
Electron MSAL Node.js msal-node Library can request ID tokens for user sign-in. Library can request access tokens for protected web APIs. Openbare preview
Java MSAL4J msal4j Library can request ID tokens for user sign-in. Library can request access tokens for protected web APIs. GA
macOS (Swift/Obj-C) MSAL voor iOS en macOS MSAL Zelfstudie Library can request ID tokens for user sign-in. Library can request access tokens for protected web APIs. GA
UWP MSAL.NET Microsoft.Identity.Client Zelfstudie Library can request ID tokens for user sign-in. Library can request access tokens for protected web APIs. GA
WPF MSAL.NET Microsoft.Identity.Client Zelfstudie Library can request ID tokens for user sign-in. Library can request access tokens for protected web APIs. GA

1Universele licentievoorwaarden voor onlineservices zijn van toepassing op bibliotheken in openbare preview.

Openbare clienttoepassing

Vanuit het oogpunt van code zijn desktoptoepassingen openbare clienttoepassingen. De configuratie verschilt enigszins afhankelijke van of u interactieve verificatie gebruikt of niet.

U moet MSAL.NET IPublicClientApplication bouwen en aanpassen.

IPublicClientApplication

Exclusief per code

Met de volgende code wordt een openbare clienttoepassing gestart, en gebruikers aangemeld in de openbare cloud van Microsoft Azure, met een werk- of schoolaccount, of met hun persoonlijke Microsoft-account.

IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId)
    .Build();

Als u van plan bent om interactieve verificatie of apparaatcodestroom te gebruiken, gebruikt u de wijzigingsfunctie .WithRedirectUri, zoals eerder getoond.

IPublicClientApplication app;
app = PublicClientApplicationBuilder.Create(clientId)
        .WithDefaultRedirectUri()
        .Build();

Configuratiebestanden gebruiken

Met de volgende code wordt een openbare clienttoepassing gestart vanuit een configuratieobject. Dit kan programmatisch kan worden ingevuld of uit een configuratiebestand worden gelezen.

PublicClientApplicationOptions options = GetOptions(); // your own method
IPublicClientApplication app = PublicClientApplicationBuilder.CreateWithApplicationOptions(options)
        .WithDefaultRedirectUri()
        .Build();

Uitgebreidere configuratie

U kunt de toepassing uitbouwen door een aantal wijzigingsfuncties toe te voegen. Als u bijvoorbeeld wilt dat uw toepassing een toepassing met meerdere tenants is in een nationale cloud (zoals de Amerikaanse overheid in dit voorbeeld), kunt u het volgende schrijven:

IPublicClientApplication app;
app = PublicClientApplicationBuilder.Create(clientId)
        .WithDefaultRedirectUri()
        .WithAadAuthority(AzureCloudInstance.AzureUsGovernment,
                         AadAuthorityAudience.AzureAdMultipleOrgs)
        .Build();

MSAL.NET bevat ook een wijzigingsfunctie voor Active Directory Federation Services 2019:

IPublicClientApplication app;
app = PublicClientApplicationBuilder.Create(clientId)
        .WithAdfsAuthority("https://consoso.com/adfs")
        .Build();

Als u ten slotte tokens wilt verkrijgen voor een Azure AD B2C-tenant (Azure Active Directory), geeft u uw tenant op zoals wordt weergegeven in het volgende codefragment:

IPublicClientApplication app;
app = PublicClientApplicationBuilder.Create(clientId)
        .WithB2CAuthority("https://fabrikamb2c.b2clogin.com/tfp/{tenant}/{PolicySignInSignUp}")
        .Build();

Meer informatie

Voor meer informatie over het configureren van een MSAL.NET desktoptoepassing:

Volledig voorbeeld met configuratieopties

Stel u een .NET-consoletoepassing voor met het volgende appsettings.json configuratiebestand:

{
  "Authentication": {
    "AzureCloudInstance": "AzurePublic",
    "AadAuthorityAudience": "AzureAdMultipleOrgs",
    "ClientId": "00001111-aaaa-2222-bbbb-3333cccc4444"
  },

  "WebAPI": {
    "MicrosoftGraphBaseEndpoint": "https://graph.microsoft.com"
  }
}

U hebt weinig code om in dit bestand te lezen met behulp van het via .NET geleverde configuratieframework:

public class SampleConfiguration
{
 /// <summary>
 /// Authentication options
 /// </summary>
 public PublicClientApplicationOptions PublicClientApplicationOptions { get; set; }

 /// <summary>
 /// Base URL for Microsoft Graph (it varies depending on whether the application runs
 /// in Microsoft Azure public clouds or national or sovereign clouds)
 /// </summary>
 public string MicrosoftGraphBaseEndpoint { get; set; }

 /// <summary>
 /// Reads the configuration from a JSON file
 /// </summary>
 /// <param name="path">Path to the configuration json file</param>
 /// <returns>SampleConfiguration as read from the json file</returns>
 public static SampleConfiguration ReadFromJsonFile(string path)
 {
  // .NET configuration
  IConfigurationRoot Configuration;
  var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile(path);
  Configuration = builder.Build();

  // Read the auth and graph endpoint configuration
  SampleConfiguration config = new SampleConfiguration()
  {
   PublicClientApplicationOptions = new PublicClientApplicationOptions()
  };
  Configuration.Bind("Authentication", config.PublicClientApplicationOptions);
  config.MicrosoftGraphBaseEndpoint =
  Configuration.GetValue<string>("WebAPI:MicrosoftGraphBaseEndpoint");
  return config;
 }
}

Schrijf nu de volgende code om uw toepassing te maken:

SampleConfiguration config = SampleConfiguration.ReadFromJsonFile("appsettings.json");
var app = PublicClientApplicationBuilder.CreateWithApplicationOptions(config.PublicClientApplicationOptions)
           .WithDefaultRedirectUri()
           .Build();

Voordat de aanroep bij de .Build() methode wordt uitgevoerd, kunt u de configuratie overschrijven met aanroepen naar .WithXXX methoden, zoals eerder getoond.

Volgende stappen

Ga verder met het volgende artikel in dit scenario: Een token ophalen voor de desktop-app.