Создайте пользователя с помощью API-интерфейса Azure AD Graph в Azure AD B2C - PullRequest
3 голосов
/ 19 марта 2019

Я пытаюсь создать пользователя в своем каталоге Azure AD B2C. Я успешно создал пользователя, опубликовав образец файла JSON с помощью этой команды в cmd:

>B2C Create-User <Path to my JSON file>

Я пытаюсь опубликовать некоторую строку JSON вместо файла JSON, и командная строка выдаст мне следующую ошибку:

Error Calling the Graph API:
{
  "odata.error": {
    "code": "Request_BadRequest",
    "message": {
      "lang": "en",
      "value": "A value without a type name was found and no expected type is available. When the model is specified, each value in the payload must have a type which can be either specified in the payload, explicitly by the caller or implicitly inferred from the parent value."
    },
    "requestId": "965dba69-4488-4d24-88a4-989a6d06cd8d",
    "date": "2019-03-19T12:55:27"
  }
}

Вот как я пытаюсь опубликовать JSON:

private static void CreateUser(string[] args)
    {

        string json = new JavaScriptSerializer().Serialize(new
        {
            accountEnabled = true,
            SignInName = new List<UserInformation.SignInName>()
            {
                new UserInformation.SignInName(){ type = "emailAddress", value = "safanmomin92@gmail.com" }
            },
            creationType = "LocalAccount",
            displayName = "Safan Momin",
            mailNickname = "Safan",
            passwordProfile = new UserInformation.PasswordProfile
            {
                password = "Mazik@123", forceChangePasswordNextLogin = false
            },
            passwordPolicies = "DisablePasswordExpiration"

        });

        object formatted = JsonConvert.DeserializeObject(client.CreateUser(json).Result);
    }

Это мой метод CreateUser:

 public async Task<string> CreateUser(string json)
    {
        return await SendGraphPostRequest("/users", json);
    }

Это мой метод SendGraphPostRequest:

private async Task<string> SendGraphPostRequest(string api, string json)
        {
            // NOTE: This client uses ADAL v2, not ADAL v4
            AuthenticationResult result = authContext.AcquireToken(Globals.aadGraphResourceId, credential);
            HttpClient http = new HttpClient();
            string url = Globals.aadGraphEndpoint + tenant + api + "?" + Globals.aadGraphVersion;

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("POST " + url);
            Console.WriteLine("Authorization: Bearer " + result.AccessToken.Substring(0, 80) + "...");
            Console.WriteLine("Content-Type: application/json");
            Console.WriteLine("");
            Console.WriteLine("JSON: "+json);
            Console.WriteLine("");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await http.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                string error = await response.Content.ReadAsStringAsync();
                object formatted = JsonConvert.DeserializeObject(error);
                throw new WebException("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine((int)response.StatusCode + ": " + response.ReasonPhrase);
            Console.WriteLine("");

            return await response.Content.ReadAsStringAsync();
        }

1 Ответ

1 голос
/ 19 марта 2019

Я сравнивал мою строку JSON и нашел там ошибку.Я писал SignInName вместо signInNames.

...