<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-M74D8PB" height="0" width="0" style="display:none;visibility:hidden">
Loading
Skip to NavigationSkip to Main Content
0D51Y00006feufsSAAOkta Classic EngineIntegrationsAnswered2024-04-15T10:02:01.000Z2019-09-18T21:29:16.000Z2019-10-24T18:41:22.000Z
OKTA API Create User Issue with .NET

I am working with OKTA User API and trying to Create/Update user from .NET Core service. But i am getting "The request body was not well-formed". The below code i used to make API call to OKTA.

 

public static class ApiHelper

  {

    public static void AddHeaders(HttpRequestHeaders headers, string authType = "", string secret = "")

    {

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

      if (!string.IsNullOrWhiteSpace(authType) && !string.IsNullOrWhiteSpace(secret))

      {

        headers.Authorization = new AuthenticationHeaderValue(authType, secret);

      }

    }

 

    public static async Task<T> GetAsync<T>(string url, string authType = "", string secret = "")

    {

      using (var client = new HttpClient())

      {

        var request = new HttpRequestMessage

        {

          Method = HttpMethod.Get,

          RequestUri = new Uri(url)

        };

        AddHeaders(request.Headers, authType, secret);

        var response = await client.SendAsync(request);

        var result = response.Content.ReadAsStringAsync().Result;

        return JsonConvert.DeserializeObject<T>(result);

      }

    }

 

    public static async Task<TResult> PostAsync<TResult, TResponse>(string url, TResponse data, string authType = "", string secret = "")

    {

      using (var client = new HttpClient())

      {

        var request = new HttpRequestMessage

        {

          Method = HttpMethod.Post,

          RequestUri = new Uri(url)

        };

        AddHeaders(request.Headers, authType, secret);

        request.Content = new JsonContent(data);

        var response = await client.SendAsync(request);

        var result = response.Content.ReadAsStringAsync().Result;

        return JsonConvert.DeserializeObject<TResult>(result);

      }

    }

  }

 

  public class JsonContent : StringContent

  {

    public JsonContent(object payload) :

      base( JsonConvert.SerializeObject(payload), Encoding.Unicode, "application/json")

    {

    }

  }

 

Note: Get data from OKTA is working, but POST data to OKTA is not working.

POST URL - https://<companyapp>.oktapreview.com/api/v1/users?activate=true

JSON: {

"id": null,

"status": null,

 "profile": {

  "firstName": "demo",

  "lastName": "user",

  "email": "demo.user@company.com",

  "login": "demo.user@company.com",

  "department": "Art",

  "mobilePhone": "1111111111"

 }

}

 

I tried in POSTMAN the same data is working. It is not working in .NET Core. Please look into this and provide a fix/workaround asap.

 

 

 

 


  • Hello Theresa,

     

    Thank you for posting in the community forum. Looking at the data being passed to the Okta itself which has required the First name, Last name, username and email. Kindly open a case with Okta support and we can have one of our developer support Engineer to assist you with the .net portion of the code and provide assistance.

     

    Thank you,

    Ashwin

    Expand Post
    Selected as Best
  • Hello Theresa,

     

    Thank you for posting in the community forum. Looking at the data being passed to the Okta itself which has required the First name, Last name, username and email. Kindly open a case with Okta support and we can have one of our developer support Engineer to assist you with the .net portion of the code and provide assistance.

     

    Thank you,

    Ashwin

    Expand Post
    Selected as Best
This question is closed.
Loading
OKTA API Create User Issue with .NET