I am trying to create users from ASP.NET Core 5 Web API project for Auth0 , was going through this official doc to make it work - Create User. Everything looks good through I am continuously getting the status code 400 and from the doc, the status code may indicate the followings:
400 - Invalid request body. The message will vary depending on the cause.
400 - Connection does not support user creation through the API. It must either be a database or passwordless connection.
400 - Cannot set username for connection without requires_username.
400 - Connection does not exist.
400 - Connection is disabled.
I’ve the connection name as NewDbConn-2024 and in the code snippet used it as follows according to the doc:
var content = new StringContent("{\"email\":\"sample1234@yahoo.com\",\"phone_number\":\"\",\"user_metadata\":{},\"blocked\":false,\"email_verified\":false,\"phone_verified\":false,\"app_metadata\":{},\"given_name\":\"sample12\",\"family_name\":\"sample12\",\"name\":\"sample12\",\"nickname\":\"sample12\",\"picture\":\"\",\"user_id\":\"\",\"connection\":\"NewDbConn-2024\",\"password\":\"\",\"verify_email\":false,\"username\":\"sample-2024\"}", null, "application/json");
Here’s the full code:
[HttpPost("CreateUserAuth0")]
public async Task<IActionResult> CreateUserAuth0()
{
//
// Add authorization access token.
string accessToken = _auth0Service.GetAccessToken();
string authorization = $"Bearer {accessToken}";
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://company-domain.us.auth0.com/api/v2/users");
request.Headers.Add("Accept", "application/json");
request.Headers.Add("authorization", authorization);
var content = new StringContent("{\"email\":\"sample1234@yahoo.com\",\"phone_number\":\"\",\"user_metadata\":{},\"blocked\":false,\"email_verified\":false,\"phone_verified\":false,\"app_metadata\":{},\"given_name\":\"sample12\",\"family_name\":\"sample12\",\"name\":\"sample12\",\"nickname\":\"sample12\",\"picture\":\"\",\"user_id\":\"\",\"connection\":\"NewDbConn-2024\",\"password\":\"\",\"verify_email\":false,\"username\":\"sample-2024\"}", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
return Ok(response.Content);
}
I am not sure if there’s anything that I am missing in the body or even the connection name, though the connection is created in the dashboard.
Tried something as follows, creating a separate object. But getting the same exception or status:
[HttpPost("CreateUserAuth0")]
public async Task<IActionResult> CreateUserAuth0()
{
//
//Add authorization access token.
string accessToken = _auth0Service.GetAccessToken();
string authorization = $ "Bearer {accessToken}";
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://company-domain.us.auth0.com/api/v2/users");
request.Headers.Add("Accept", "application/json");
request.Headers.Add("authorization", authorization);
var data = new CreateAuth0UserModel
{
Email = "sample1234@yahoo.com",
Blocked = false,
EmailVerified = true,
PhoneVerified = false,
GivenName = "Sample12",
FamilyName = "Sample12",
Name = "Amit",
Connection = "NewDbConn-2024",
Password = "",
VerifyEmail = false,
Username = "Sample12"
};
var json = JsonConvert.SerializeObject(data);
var content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
return Ok(response.Content);
}