A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/78811990/how-to-authenticate-to-azure-ad-using-java-sdk below:

How to authenticate to Azure AD using java SDK

You can make use of Microsoft Graph Java SDK to authenticate Azure AD for user's addition and removal.

Initially, I registered one application and granted User.ReadWrite.All permission of Application type with consent as below:

Now, I created one client secret in above app registration and noted it's value like this:

In my case, I ran below sample code to create user in Azure AD and got response like this:

Main.java:

package org.example;

import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.User;
import com.microsoft.graph.models.PasswordProfile;
import com.microsoft.graph.requests.GraphServiceClient;
import okhttp3.Request;
import java.util.Arrays;
import java.util.List;

public class Main {
    private static final String CLIENT_ID = "appId";
    private static final String CLIENT_SECRET = "secretValue";
    private static final String TENANT_ID = "tenantId";

    public static void main(String[] args) {
        try {
            ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                    .clientId(CLIENT_ID)
                    .clientSecret(CLIENT_SECRET)
                    .tenantId(TENANT_ID)
                    .build();

            List<String> scopes = Arrays.asList("https://graph.microsoft.com/.default");
            TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(
                    scopes,
                    credential
            );

            GraphServiceClient<Request> graphClient = GraphServiceClient
                    .builder()
                    .authenticationProvider(authProvider)
                    .buildClient();

            createUser(graphClient);

            // delete a user
            // deleteUser(graphClient, "<USER_ID>");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createUser(GraphServiceClient<Request> graphClient) {
        User user = new User();
        user.displayName = "User Name";
        user.mailNickname = "username";
        user.userPrincipalName = "[email protected]";
        user.accountEnabled = true;
        user.passwordProfile = new PasswordProfile();
        user.passwordProfile.password = "P@ssw0rd!";
        user.passwordProfile.forceChangePasswordNextSignIn = false;

        User createdUser = graphClient.users()
                .buildRequest()
                .post(user);

        System.out.println("Created User with ID: " + createdUser.id);
        System.out.println("Created User with Name: " + createdUser.displayName);
    }

    private static void deleteUser(GraphServiceClient<Request> graphClient, String userId) {
        graphClient.users(userId)
                .buildRequest()
                .delete();
        System.out.println("Deleted User with ID: " + userId);
    }
}

Response:

To confirm that, I checked the same in Portal where new user created successfully as below:

Before executing the code above, ensure that the necessary dependencies are installed in your Java project:

pom.xml:

<dependencies>
        <!-- Azure Identity library for authentication -->
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.6.0</version>
        </dependency>
        <!-- Microsoft Graph SDK for Java -->
        <dependency>
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph</artifactId>
            <version>5.0.0</version>
        </dependency>
        <!-- Logging -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.6</version>
        </dependency>
</dependencies>

Reference:

Create User - Microsoft Graph v1.0


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4