Zum Inhalt springen
AdminLabs

AdminLabs

Tools, FAQs & mehr

  • Home
  • RDSMan
  • iOS Apps
    • MFA App
    • Notdienst
    • Musikverein
  • Linksammlung
  • Blog

Anmeldung mit Microsoft AzureAD/EntraID – PHP Anwendung

31. Juli 202330. Juli 2022 von Florian

Beispielcode für die Authentifizierung bzw. Anmeldung mit Microsoft / AzureAD / EntraID an einer eigenen PHP-Webanwendung. (Delegated Permissions)

Es zeigt lediglich die Basis, wie eine Anmeldung über Microsoft möglich ist. Dieser Code sollte nicht 1:1 in eine Produktivumgebung übernommen werden, da kein Errorhandling, etc. vorhanden ist. Einzelne Elemente zeigen jedoch, wie sich die Kommunikation aufbauen lässt.

Vorbereitung: App im Azure Portal registrieren

Gemäß den Schritten in den Screenshots unter https://portal.azure.com unter App-Registrierungen eine neue App erstellen, und App-ID als CLIENT_ID und Wert unter Schlüssel als CLIENT_SECRET in den PHP-Code übernehmen:

PHP Code

Folgenden Code im Webspace abgelgen und die REDIRECT_URI auf den Pfad der PHP-Datei anpassen. Dieser muss dem angegebenen Pfad der App-Registrierung entsprechen.

<?php
//
//  very Basic oAuth2 Microsoft AzureAD/EntraID PHP PoC Example-Code
//  (c) Florian Neumann, 2022
//  https://www.adminlabs.de/anmeldung-mit-microsoft-azuread-php-anwendung/
//
//  https://docs.microsoft.com/en-us/previous-versions/azure/dn645542(v=azure.100)
//  https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http
//

$CLIENT_ID      = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
$CLIENT_SECRET  = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
$REDIRECT_URI   = "https://mydomain.tld/msoauth.php";

//
// build and output Login-URL with App-Details
//
$parameter = http_build_query(array(
    "response_type" => "code",
    "client_id"     => $CLIENT_ID,
    "redirect_uri"  => $REDIRECT_URI
));

echo "<a href='https://login.microsoftonline.com/common/oauth2/authorize?" . $parameter . "'>Login</a><br><br>";

// // if return Authorization Code by Microsoft, query Access Token // if($_GET['code'] != "") {     echo "Authorization Code received: " . $_GET['code'] . "<br><br>";     $parameter = http_build_query(array(         "grant_type"    => "authorization_code",         "client_id"     => $CLIENT_ID,         "client_secret" => $CLIENT_SECRET,         "code"          => $_GET['code'],         "redirect_uri"  => $REDIRECT_URI,         "resource"      => "https://graph.microsoft.com/"     ));     //     // got Authorization Code, query Access Token from Microsoft with this code     //     $MSAADAuthSession = curl_init();     curl_setopt($MSAADAuthSession ,CURLOPT_URL, "https://login.microsoftonline.com/common/oauth2/token");     curl_setopt($MSAADAuthSession, CURLOPT_SSL_VERIFYHOST, 2);     curl_setopt($MSAADAuthSession, CURLOPT_RETURNTRANSFER, TRUE);     curl_setopt($MSAADAuthSession, CURLOPT_CUSTOMREQUEST, "POST");     curl_setopt($MSAADAuthSession, CURLOPT_VERBOSE, 0);       curl_setopt($MSAADAuthSession, CURLOPT_POSTFIELDS, $parameter);     curl_setopt($MSAADAuthSession, CURLOPT_HTTPHEADER, array('Content-Type:application/x-www-form-urlencoded'));     $result = json_decode(curl_exec($MSAADAuthSession));     echo "HTTP Status-Code: " . curl_getinfo($MSAADAuthSession, CURLINFO_HTTP_CODE) . "<br><br>";     curl_close($MSAADAuthSession);     var_dump($result);     echo "<br><br>Access Token: " . $result->access_token . "<br><br><hr>";     //     // got Access Token, query personal Details from GraphAPI     //     $MSGraphAPISession = curl_init();     curl_setopt($MSGraphAPISession, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me");     curl_setopt($MSGraphAPISession, CURLOPT_SSL_VERIFYHOST, 2);     curl_setopt($MSGraphAPISession, CURLOPT_RETURNTRANSFER, TRUE);     curl_setopt($MSGraphAPISession, CURLOPT_CUSTOMREQUEST, "GET");     curl_setopt($MSGraphAPISession, CURLOPT_HTTPHEADER, array(       'Content-Type:application/json',       'Authorization: Bearer ' . $result->access_token     ));     $result = json_decode(curl_exec($MSGraphAPISession));     echo "HTTP Status-Code: " . curl_getinfo($MSGraphAPISession, CURLINFO_HTTP_CODE) . "<br><br>";     curl_close($MSGraphAPISession);     //     // Output User Details logged in     //     var_dump($result);     echo "<hr>";     echo $result->userPrincipalName . "<br>";     echo $result->givenName . "<br>"; } ?>
Kategorien Blog Schlagwörter anmeldung, authentication, azure, azure app registration, azure portal, azuread, login, microsoft, oauth, oauth2, php, webanwendung, webapplication
Home Assistant Corona-Zahlen
Mennekes Amtron Wallbox API auslesen

Neueste Beiträge

  • Mennekes Amtron Wallbox API auslesen
  • Anmeldung mit Microsoft AzureAD/EntraID – PHP Anwendung
  • Home Assistant Corona-Zahlen
  • Home Assistant Internet Speedtest
  • NextCloud mit Sophos UTM Webserver Protection
  • Impressum
  • Datenschutz
© 2025 Florian Finder
Diese Website nutzt Cookies um bestimmte Funktionen bereitzustellen und die bestmögliche Nutzererfahrung zu ermöglichen. Mit Klick auf "Akzeptieren" werden alle Cookies erlaubt.
Cookie EinstellungenAkzeptieren
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
immer aktiv
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SPEICHERN & AKZEPTIEREN