6. Codebeispiele

a. Erstellen eines Authentifizierungstokens


Erstellen eines Authentifizierungstokens
public static async Task<string> CreateToken(string licenseIndicatorHash)
{
const string url = $"{baseUrl}oauth2/token";
var nvc = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type","client_credentials"),
new KeyValuePair<string, string>("scope", licenseIndicatorHash),
new KeyValuePair<string, string>("requesttype", TestSetupData.Requesttype)
};
var content = new FormUrlEncodedContent(nvc);
var response = await HttpClient.PostAsync(url, content);
 
dynamic token = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
return token.access_token;
}


b. Abholen der Jobs

Abholen der Jobs
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
 
builder = new UriBuilder($"{baseUrl}jobs");
 
query = HttpUtility.ParseQueryString(builder.Query);
 
builder.Query = query.ToString();
 
url = builder.ToString();
 
response = await HttpClient.GetAsync(url);

c. Bestätigen der Jobs

Bestätigen der Jobs
//get the confirmationToken dynamic confirmationToken;
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
 
var requestContent = new StringContent("", Encoding.UTF8, "application/json");
 
var jobConfirmationResponse = await HttpClient.PostAsync($"{baseUrl}jobs/{job.id}/confirmations", requestContent);
 
confirmationToken = JsonConvert.DeserializeObject(jobConfirmationResponse.Content.ReadAsStringAsync().Result);

d. Versand der Responses

Versand der Responses
var salt = "ChangeMeToTheRealSalt";
 
dynamic postData = new
{
response = TestSetupData.GetStaticJobData1Encrypted(TestSetupData.LicenseCounterpartSecret, salt),
responsecode = "TestCode-0001",
isResponseOk = “true”,
confirmationToken,
salt
};
 
// add the authorization header
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
 
string strJson = JsonConvert.SerializeObject(postData);
 
var content = new StringContent(strJson, Encoding.UTF8, "application/json");
 
var responseJob = await HttpClient.PostAsync($"{baseUrl}jobs/{job.id}/responses", content);
 
var savedJob = JsonConvert.DeserializeObject(responseJob.Content.ReadAsStringAsync().Result);

e. Ver-/Entschlüsselung der Daten

Ver-/Entschlüsselung der Daten
string encryptedData;
using (var cryptoManager = new CryptoServices.CryptoManager(new NetworkCredential("", licenseSecret).SecurePassword, salt))
{
var encryptedByte = cryptoManager.Encrypt(deserializedContent.ToString());
encryptedData = Convert.ToBase64String(encryptedByte);
}
return encryptedData;
 
using (var cryptoManager = new CryptoServices.CryptoManager(new NetworkCredential("", licenseSecret).SecurePassword, salt))
{
return cryptoManager.Decrypt(Convert.FromBase64String(deserializedEncryptedBase64Content.T oString()));
}