public IHttpActionResult PostADUsers([FromBody] GUIReceivedUserJSONModel guiReceivedUserJSONModel) { bool result = AddUsersToAdAndDb(guiReceivedUserJSONModel); if (result) { return(Ok()); } else { return(InternalServerError()); } }
public bool AddUsersToAdAndDb(GUIReceivedUserJSONModel guiReceivedUserJSONModel) { ADUserGraphTokenResponse aDUserGraphTokenResponse = GenerateAccessToken(); ADUserJsonModel adUserJson = new ADUserJsonModel(); HttpClient graphCRUDClient = new HttpClient(); string responseString = ""; Task.Run(async() => { graphCRUDClient.BaseAddress = new Uri("https://graph.microsoft.com/v1.0/users"); graphCRUDClient.DefaultRequestHeaders.Accept.Clear(); graphCRUDClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + aDUserGraphTokenResponse.AccessToken); graphCRUDClient.DefaultRequestHeaders.Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); //message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", aDUserGraphTokenResponse.AccessToken); Random rnd = new Random(); int num1 = rnd.Next(0, 9); int num2 = rnd.Next(0, 9); int num3 = rnd.Next(0, 9); int num4 = rnd.Next(0, 9); Models.ADModels.PasswordProfile passwordProfile = new Models.ADModels.PasswordProfile() { Password = "******", ForceChangePasswordNextSignIn = true }; GraphAddUserJSONModel graphUser = new GraphAddUserJSONModel() { AccountEnabled = true, DisplayName = guiReceivedUserJSONModel.FirstName + guiReceivedUserJSONModel.LastName, GivenName = guiReceivedUserJSONModel.FirstName, Surname = guiReceivedUserJSONModel.LastName, MobilePhone = guiReceivedUserJSONModel.PhoneNumber, MailNickname = guiReceivedUserJSONModel.FirstName + guiReceivedUserJSONModel.LastName.Substring(0, 1), UserPrincipalName = guiReceivedUserJSONModel.FirstName.ToLower() + "." + guiReceivedUserJSONModel.LastName.ToLower() + Convert.ToString(num1) + Convert.ToString(num2) + Convert.ToString(num3) + Convert.ToString(num4) + "@andresgllive764.onmicrosoft.com", PasswordPolicies = "DisablePasswordExpiration", PasswordProfile = passwordProfile }; string postBody = JsonConvert.SerializeObject(graphUser); var content = new StringContent(postBody, Encoding.UTF8, "application/json"); HttpResponseMessage response = await graphCRUDClient.PostAsync("", content); responseString = await response.Content.ReadAsStringAsync(); }).Wait(); try { JToken parsed = JToken.Parse(responseString); adUserJson.Id = parsed["id"].Value <string>(); adUserJson.DisplayName = parsed["displayName"].Value <string>(); adUserJson.GivenName = parsed["givenName"].Value <string>(); adUserJson.JobTitle = parsed["jobTitle"].Value <string>(); adUserJson.Mail = parsed["mail"].Value <string>(); adUserJson.MobilePhone = parsed["mobilePhone"].Value <string>(); adUserJson.OfficeLocation = parsed["officeLocation"].Value <string>(); adUserJson.PreferredLanguage = parsed["preferredLanguage"].Value <string>(); adUserJson.surname = parsed["surname"].Value <string>(); adUserJson.UserPrincipalName = parsed["userPrincipalName"].Value <string>(); Models.Contact contact = new Models.Contact() { email = guiReceivedUserJSONModel.Email, objectId = adUserJson.Id, firstName = adUserJson.GivenName, lastName = adUserJson.surname, phoneNumber = adUserJson.MobilePhone }; db.Contacts.Add(contact); db.SaveChanges(); return(true); } catch { return(false); } }