Quantcast
Channel: Azure Active Directory forum
Viewing all articles
Browse latest Browse all 16000

Use .NET HttpClient AND Graph URL to create user

$
0
0

Hi,

I am trying to using .NET HttpClient And Graph API to create a Azure AD user, I got some trouble.

Blow is my code:

 HttpClient client = new HttpClient();
            string feedUrl = this.generaterFeedUrl("users");
            this.setClientHeaders(client);
            HttpContent postContent = new StringContent("{\"accountEnabled\": true,"+"\"displayName\": \"Alex Wu\","+"\"mailNickname\": \"AlexW\","+"\"passwordProfile\": {"+"\"password\": \"Test1234\","+"\"forceChangePasswordNextLogin\": "+ false +"},"+"\"userPrincipalName\": \"Alex@tenant.onmicrosoft.com\""+"}", UTF8Encoding.UTF8, "application/json");
            var response = client.PostAsync(feedUrl, postContent);
            if (response.Result.StatusCode.ToString() == "OK")
            {
                string result = response.Result.Content.ReadAsStringAsync().Result;
                JObject resultObject = (JObject)JsonConvert.DeserializeObject(result);
...
setClientHeaders function:
 public void setClientHeaders(HttpClient client)
        {
            client.DefaultRequestHeaders.Add("Authorization", String.Format("{1} {0}", this.getAccessToken(), this.TokenType));
            client.DefaultRequestHeaders.Add("Accept", "application/json;odata=minimalmetadata");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //client.DefaultRequestHeaders.Add("Content-Type", "application/json;odata=minimalmetadata");
            client.DefaultRequestHeaders.Add("Prefer", "return-content");

        }

Run the code. I got an error:

{"odata.error":{"code":"Request_BadRequest","message":{"lang":"en","value":"Invalid JSON. A token was not recognized in the JSON content."}}}

How to fix this issue?

Thanks all your hlep!


Viewing all articles
Browse latest Browse all 16000

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>