Advertisement
Guest User

Untitled

a guest
Sep 11th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. //Get access token (using ADAL)
  2. var authenticationContext = new AuthenticationContext(AuthString, false);
  3. var clientCred = new ClientCredential(ClientId, ClientSecret);
  4. var authenticationResult = authenticationContext.AcquireTokenAsync(ResourceUrl, clientCred);
  5. var token = authenticationResult.Result.AccessToken;
  6.  
  7.  
  8. //HTTP POST CODE
  9. const string mail = "new@email.com";
  10. // Create a new user object.
  11. var user = new CustomUser
  12. {
  13. accountEnabled = true,
  14. country = "MS",
  15. creationType = "LocalAccount",
  16. displayName = mail,
  17. passwordPolicies = "DisablePasswordExpiration,DisableStrongPassword",
  18. passwordProfile = new passwordProfile { password = "jVPmEm)6Bh", forceChangePasswordNextLogin = true },
  19. signInNames = new signInNames { type = "emailAddress", value = mail }
  20. };
  21.  
  22. var url = "https://graph.windows.net/" + TenantId + "/users?api-version=1.6";
  23.  
  24. var jsonObject = JsonConvert.SerializeObject(user);
  25.  
  26. using (var client = new HttpClient())
  27. {
  28. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  29.  
  30. var response = client.PostAsync(url,
  31. new StringContent(JsonConvert.SerializeObject(user).ToString(),
  32. Encoding.UTF8, "application/json"))
  33. .Result;
  34.  
  35. if (response.IsSuccessStatusCode)
  36. {
  37. dynamic content = JsonConvert.DeserializeObject(
  38. response.Content.ReadAsStringAsync()
  39. .Result);
  40.  
  41. // Access variables from the returned JSON object
  42. var appHref = content.links.applications.href;
  43. }
  44. }
  45.  
  46. {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:....}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement