# Anmeldung an einer Desktop-Anwendung

Der nachfolgende C#-Code zeigt beispielhaft, wie der Zugriffstoken für ELO Sync bei einer .NET Core Desktopanwendung ermittelt werden kann:

static async Task<string> SignInUserAndGetTokenUsingMSAL(string[] scopes)
{
   string authority = $"<instance>/<tenant-id>"; // z.B. https://login.microsoftonline.com/<tenant-id>
   // Initialisierung der MSAL library
   IPublicClientApplication application = PublicClientApplicationBuilder.Create("<own-client-id>")
                                             .WithAuthority(authority)
                                             .WithDefaultRedirectUri()
                                             .Build();
   AuthenticationResult result;
   try
   {
      // Wenn bereits eine Anmeldung durchgeführt wurde, dann wird diese wiederverwendet.
      var accounts = await application.GetAccountsAsync();
      result = await application.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
         .ExecuteAsync();
   }
   catch (MsalUiRequiredException ex)
   {
      // Wenn noch keine Anmeldung durchgeführt wurde, wird ein Browserfenster geöffnet, in dem die Anmeldedaten eingegeben werden können. Nach erfolgreicher Anmeldung wird das Ergebnis mit dem Zugriffstoken zurückgeliefert.
      result = await application.AcquireTokenInteractive(scopes)
         .WithClaims(ex.Claims)
         .ExecuteAsync();
   }
   return result.AccessToken;
}
string[] scopes = new string[] { "<elosync-client-id>/.default" };
string bearerToken = await SignInUserAndGetTokenUsingMSAL(scopes);

Danach gibt die Anmeldung ein Zugriffstoken für die Anwendung zurück, das bei jeder Anfrage an ELO Sync verwendet werden muss, indem es im HTTP-Header Authorization als Bearer gesendet wird.

Für weitere Informationen siehe Microsoft identity platform code samples for authentication and authorization (opens new window)

Zuletzt aktualisiert: 11. Juli 2025 um 06:58