简体   繁体   中英

Create Azure AD user by tenant and app ID

How I can create a user by Client secrets in Azure AD with PHP?

I need access token in below code to create a user. To have this token I need to login first. How I can create a user automatically without any login.

curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS =>'{
                  "accountEnabled": true,
                  "displayName": "Adele Vance",                  
                  "userPrincipalName": "adelev2@xxx.net",
                  "passwordProfile" : {
                    "forceChangePasswordNextSignIn": true,
                    "password": "xWwvJ]6NMw+bWH-d"
          }
        }',
          CURLOPT_HTTPHEADER => array(
            "Authorization: Bearer $accessToken",
            "Content-Type: application/json"
          ),
        ));

You can refer to this sample , which uses a daemon that does not require user login, and uses the client credential flow to obtain an access token to call MS graph api to create a user. You need to grant User.ReadWrite.All application permissions for the application.

在此处输入图像描述

With special thanks to Carl which provide useful links I did it by using two below functions:

I receive a token by calling getToken function and use it in getToken to create a user without any previous login.


function getToken() {
       
    $curl = curl_init();
    
    $dir = env('OAUTH_DIR_ID');
    $clientId = env('OAUTH_APP_ID');
    $secretKey = env('OAUTH_APP_PASSWORD');
    
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://login.microsoftonline.com/$dir/oauth2/v2.0/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => "client_id=$clientId&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=$secretKey&grant_type=client_credentials",
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/x-www-form-urlencoded',
            'x-ms-gateway-slice=estsfd; stsservicecookie=estsfd'
        ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
    
}

function addUser($accessToken)
{
    try {

        $curl = curl_init();
        
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS =>'{
                  "accountEnabled": true,
                  "displayName": "Adele Vance",                  
                  "userPrincipalName": "adelev2@yoed.net",
                  "passwordProfile" : {
                    "forceChangePasswordNextSignIn": true,
                    "password": "xWwvJ]6NMw+bWH-d"
          }
        }',
          CURLOPT_HTTPHEADER => array(
            "Authorization: Bearer $accessToken",
            "Content-Type: application/json"
          ),
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
        
        var_dump($response); // Debug print
        exit();
        
        
    } catch (Error $ex) {
        $home = env('APP_URL');
        header("Location: $home/signin.php?err=" . $ex->getMessage());
        die();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM