ewomack ewomack - 1 year ago 151
JSON Question

Receiving BAD REQUEST when called Okta API from C# MVC application

I am creating a sample application (i.e., a proof of concept) for creating users with the Okta platform. I am using API calls but consistently receiving "BAD REQUEST" when running the C# MVC application from Visual Studio 2013 update 5 to my Okta development instance. I'm wondering if the problem is between CORS and a local app?

Here is what I have so far:

  • Tested out the API calls using Postman to my dev environment and the calls work (i.e., users get created in my Okta dev admin environment)

  • Created an API Token and call it with a prefix of "SSWS" in the Authorization header

  • Using an
    method to make the API call

  • My application code works as expected when calling a GET with the API call

  • Using the following Api call: /api/v1/users?activate=false (create a user with password; this works in Postman, but not in the MVC app)

  • Used http://json2csharp.com/ to create C# classes that conform to Okta's JSON hierarchy (obtained from Okta's Postman API libraries)

  • Using the classes above, the JSON displayed in Visual Studio's Text Viewer (obtained while stepping through the code) works with a POST call when pasted into Postman

  • HttpResponse
    contains the error message "The request body was not well-formed"

Here is the code used for creating and serializing (with Json.NET) the C# classes:

RootObject root = new RootObject();
root.profile = new Profile();
root.profile.firstName = model.FirstName;
root.profile.lastName = model.LastName;
root.profile.email = model.Email;
root.profile.login = model.UserName;

root.credentials = new Credentials();
root.credentials.password = new OktaTest.Models.Password();
root.credentials.password.value = model.Password;

string rootJson = JsonConvert.SerializeObject(root);

This produces the following JSON (this contains dummy data):


Here is the line of code that makes the POST call:

HttpResponseMessage responseMessage = await client.PostAsJsonAsync(url, rootJson);

Here is the line that sets the Accept header:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Since I'm able to use the JSON in Postman, and since that JSON comes out as valid when using JSONLint, I'm thinking that the problem is not with the JSON but something around security between my local app and the development environment. Should this test only be run from a hosted application so that a site can be explicitly assigned in the CORS section of the Okta admin environment? At this point, and I'm still researching and experimenting, I'm not sure what I'm missing, but I think I'm close.

Any advice or guidance would be greatly appreciated! Thank you!

Answer Source

I recommend you to use the Okta C# SDK which you can add to your application using the Okta.Core.Client NuGet package.

A sample console app shows how to use it to create Okta users: https://github.com/oktadeveloper/okta-sdk-dotnet-console-user-create

I hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download