Create a new user in Azure Active Directory (B2C) with Graph API, using http post request

后端 未结 2 416
走了就别回头了
走了就别回头了 2021-01-02 22:02

I have previously been adding users programmatically using Active Directory Authentication Library (ADAL), but now I need to define \"signInNames\" (= users email), and that

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 22:55

    Thank you for your response Fei Xue, i believe i had the right permissions. What i did to solvem my problem.

    First off i removed my own custom class "NewUser", then i downloaded this sample-project: https://github.com/AzureADQuickStarts/B2C-GraphAPI-DotNet/blob/master/B2CGraphClient/B2CGraphClient.cs to eliminate the risk that my code was wrong. I modified it to support my needs, then i created a simple JObject:

    var jsonObject = new JObject
                            {
                                {"accountEnabled", true},
                                {"country", customer.CustomerBase.Company},
                                {"creationType", "LocalAccount"},
                                {"displayName", pendingCustomer.Email.Trim()},
                                {"passwordPolicies", "DisablePasswordExpiration,DisableStrongPassword"},
                                {"passwordProfile", new JObject
                                {
                                    {"password", pwd},
                                    {"forceChangePasswordNextLogin", true}
                                } },
                                {"signInNames", new JArray
                                    {
                                        new JObject
                                        {
                                            {"value", pendingCustomer.Email.Trim()},
                                            {"type", "emailAddress"}
                                        }
                                    }
                                }
                            };
    
    client = new B2CGraphClient(ClientId, ClientSecret, TenantId);
    var response = await client.CreateUser(jsonObject.ToString());
    var newUser = JsonConvert.DeserializeObject(response);
    

    From B2CGraphClient.cs

            private async Task SendGraphPostRequest(string api, string json)
        {
            // NOTE: This client uses ADAL v2, not ADAL v4
            var result = authContext.AcquireToken(Globals.aadGraphResourceId, credential);
            var http = new HttpClient();
            var url = Globals.aadGraphEndpoint + tenant + api + "?" + Globals.aadGraphVersion;
    
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await http.SendAsync(request);
    
            if (!response.IsSuccessStatusCode)
            {
                var error = await response.Content.ReadAsStringAsync();
                var formatted = JsonConvert.DeserializeObject(error);
                //Console.WriteLine("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
                Logger.Error("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
            }
            Logger.Info((int)response.StatusCode + ": " + response.ReasonPhrase);
    
            return await response.Content.ReadAsStringAsync();
        }
    

    This finally solved all my problems, it was probably an format-error in the serialization of my NewCustomer-class, which then got rejected by the API.

提交回复
热议问题