Migrating from ADFS to Okta token generation for a WPF C# application.
As part of initial analysis we are able to generate tokens by passing the username and password with grant-type “password”. But in real time we need to integrate the AD with Okta and generate tokens based on windows login. And we need to do this in C#. Could not find any samples or okta nugets that support WPF applications.
Any help is greatly appreciated.
var token = new OktaResponse();
var client = new HttpClient();
var client_id = clientid;
var client_secret = clientsecret;
var clientCreds = System.Text.Encoding.UTF8.GetBytes($"{client_id}:{client_secret}");
client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(clientCreds));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var postMessage = new Dictionary<string, string>();
postMessage.Add("grant_type", "password");
postMessage.Add("username", username);
postMessage.Add("password", password);
postMessage.Add("scope", "openid");
var request = new HttpRequestMessage(HttpMethod.Post, $"https://trial-"+ trialid+".okta.com/oauth2/default/v1/token")
{
Content = new FormUrlEncodedContent(postMessage)
};
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var jsonSerializerSetting = new JsonSerializerSettings();
var json = await response.Content.ReadAsStringAsync();
token = JsonConvert.DeserializeObject<OktaResponse>(json, jsonSerializerSetting);
double expiresIn;
if (double.TryParse(token?.ExpiresIn, out expiresIn))
{
token.ExpiresAt = DateTime.UtcNow.AddSeconds(expiresIn);
}
}