- Home
- Authentication
- Sample Code
C#
//assigned Application Id
private const string appId = "4C782603-8B36-485A-AE33-557CF6392E44";
//authentication base url
private const string authBaseUrl = "https://openapiauthtest.intelichart.com/";
//authentication token - should be saved for up to an hour.
private static dynamic authToken = null;
private static void GetToken()
{
authToken = null;
try
{
using (var client = new HttpClient(GetHttpClientHandler()))
{
//set the base address for auth calls. This is based on environment you are calling
client.BaseAddress = new Uri(authBaseUrl);
client.DefaultRequestHeaders.Accept.Clear();
//set content type to json
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//model of json we are about to send
var authObj = new
{
ApplicationId = appId
};
//get response from server
using (var response = client.PostAsJsonAsync("api/server/token", authObj).Result)
{
var responseText = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode) //This is status code 200 - everything went well
{
authToken = JsonConvert.DeserializeObject<dynamic>(responseText);
Console.WriteLine("Token = " + authToken.token);
}
else //we have an error.
{
Console.WriteLine(response.ReasonPhrase + "\r\n" + responseText);
}
}
//clear out headers for authenticate call
client.DefaultRequestHeaders.Accept.Clear();
//set content type to json and add authorization and applicationid to header.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new
AuthenticationHeaderValue(authToken.TokenType, authToken.Token);
client.DefaultRequestHeaders.Add("ApplicationId", appId);
//authentication json object
var authenObj = new {
ApplicationId = appId,
PassCode = "ABC123",
DateOfBirth = "01/01/1945"
}
//get response from server.
using (var response = client.PostAsJsonAsync("api/server/authenticate", authenObj).Result)
{
var responseText = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode) //This is status code 200 - everything went well
{
var personUid = JsonConvert.DeserializeObject<dynamic>(responseText);
Console.WriteLine("Person Encrypted UserId = " + personUid);
//save in database for other calls.
}
else //we have an error.
{
Console.WriteLine(response.ReasonPhrase + "\r\n" + responseText);
}
}
}
}
catch (Exception ex)
{
//handle error
Console.WriteLine("Error getting token" + ex.Message);
}
}
private static HttpClientHandler GetHttpClientHandler()
{
var handler = new HttpClientHandler
{
UseCookies = false,
UseProxy = true,
UseDefaultCredentials = false
};
return handler;
}