Azure AD B2C ROPC reset password

Viewed 13

I have a Spring Boot Webservice authenticating Users over Azure AD B2C. I am trying to implement the ability for users to reset passwords, and I want to do that from my Spring Boot App. I tried doing that with an Admin User (Global Administrator) in the same directory that I created especially for that.

public void resetPassword() {
    List<String> scopes = new ArrayList<>();
    // The scopes I added in my Azure AD B2C
    scopes.add("https://example.onmicrosoft.com/exampleId/UserAuthenticationMethod.ReadWrite.All");
            scopes.add("https://example.onmicrosoft.com/example/Users.ReadWrite.All");
            GraphServiceClient client = getGraphServiceClient(new UsernamePasswordCredentialBuilder()
                    .clientId(clientId)
                    .tenantId(tenantId)
                    .username(adminUsername)
                    .password(adminPassword).build(), scopes);
            client.users(userId)
                    .authentication()
                    .passwordMethods("28c10230-6103-485e-b985-444c60001490")
                    .resetPassword(AuthenticationMethodResetPasswordParameterSet.newBuilder()
                            .withNewPassword("NEWPASSWORD123.&")
                            .withRequireChangeOnNextSignIn(false).build())
                    .buildRequest().post();
}

public GraphServiceClient getGraphServiceClient(TokenCredential tokenCredential, List<String> scopes) {
    if (tokenCredential == null) {
        tokenCredential = new ClientSecretCredentialBuilder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .tenantId(tenantId)
                .build();
    }

    if (scopes == null) {
        scopes = new ArrayList<>();
        scopes.add("https://graph.microsoft.com/.default");
    }

    final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, tokenCredential);

    return GraphServiceClient.builder()
            .authenticationProvider(tokenCredentialAuthProvider)
            .buildClient();
}

When I run that code, I get the following error:

401 Unauthorized: "{"error":{"code":"InvalidAuthenticationToken","message":"Access token validation failure. Invalid audience.","innerError":{"date":"2022-09-23T13:05:22","request-id":"1d328748-9b7f-45d8-83b4-7dbafde83cf7","client-request-id":"1d328748-9b7f-45d8-83b4-7dbafde83cf7"}}}"

I created the scopes. I gave the adminUser Global Administrater role. Any ideas what I am doind wrong?

0 Answers
Related