Search code examples
c#.netmicrosoft-graph-apiazure-ad-graph-apiflurl

Microsoft Graph API Create User in Azure AD gets 500 error


After successfully Authenticated using MSAL. I get the token and passed it on the Flurl. I'm getting 500 error. But if I test it on Postman it works properly. Anything I missed?

Below is my code:

    var user = new User
    {
        AccountEnabled = true,
        DisplayName = "Anthony Test",
        MailNickname = "AnthonyT",
        UserPrincipalName = "AnthonyT@*****.onmicrosoft.com",
        PasswordProfile = new PasswordProfile
        {
            ForceChangePasswordNextSignIn = true,
            Password = "xWwvJ]6NMw+bWH-d"
        }
    };

    var result2 = await "https://graph.microsoft.com/v1.0/users".WithHeader("Content-Type", "application/json")
        .WithOAuthBearerToken(result.AccessToken)
        .PostJsonAsync(user);

Here is a screenshot from Postman: enter image description here


Solution

  • I have fixed the issue. I forgot to set JsonProperty and make it camelCase. enter image description here