private void BtnGenerateToken_Click(object sender, RoutedEventArgs e) { string step = null; try { UpdateKeyParameter(); JwtPayload payload = new JwtPayload(); foreach (var v in JwtPayloadPairs) { step = $"Claim: {v.Name}, Value: {v.Value}"; object value; switch (v.ValueType) { case ClaimValueType.Numeric: value = long.Parse(v.Value.ToString()); break; case ClaimValueType.Decimal: value = decimal.Parse(v.Value.ToString()); break; default: value = v.Value; break; } payload.Add(v.Name, value); } string algorithm = GetAlgotithm(cbAlgorithms.SelectedItem); string token; if (algorithm.StartsWith("HS")) { string base64Key = GetBase64Key(symmetricKey, keyform); JsonWebTokenUtility.CreateHmacShaToken(base64Key, algorithm, payload, out token); } else if (algorithm.StartsWith("RS")) { var importedCertificate = ImportCertificate(certificatePath, certificatePassword); JsonWebTokenUtility.CreateRsaToken(importedCertificate, algorithm, payload, out token); } else if (algorithm.StartsWith("ES")) { var importedCertificate = ImportCertificate(certificatePath, certificatePassword); JsonWebTokenUtility.CreateEcdsaToken(importedCertificate, algorithm, payload, out token); } else { token = "The given algorithm is not supported."; } txtJwtToken.Text = token; } catch (CryptographicException ce) { ShowMessageBox("Error when doing cryptography", ce.Message, ce.ToString()); } catch (Exception ex) { ShowMessageBox("Error", "An error has occurred during generating " + step + "\nError Message: " + ex.Message, ex.ToString()); } }
/// <summary> /// Pass in the data required in order to create the Json Web Token and Consumer Search details. /// </summary> /// <param name="hpio">hpio of organisation and matches NASH certificate (mandatory)</param> /// <param name="userId">hpii of user (mandatory)</param> /// <param name="dateOfBith">(mandatory)</param> /// <param name="gender">(mandatory)</param> /// <param name="family">(mandatory)</param> /// <param name="ihi">One of 3 identifiers that can be used (conditional)</param> /// <param name="mcn">One of 3 identifiers that can be used (conditional)</param> /// <param name="dva">One of 3 identifiers that can be used (conditional)</param> /// <returns>Returns the HTML to go in a WebBrowser window. An Error will return nothing</returns> public MhrRestClientResponse GetAccessToNpp(string hpio, string userId, string dateOfBith, string gender, string family, string ihi, string mcn, string dva) { var Response = new MhrRestClientResponse(); // Certificates RSA _privateKey = _cert.GetRSAPrivateKey(); var jwt = JsonWebTokenUtility.GetNppAssertion(_client_id, _privateKey, hpio, userId, dateOfBith, gender, family, ihi, mcn, dva); var request = new RestRequest("", Method.POST); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddHeader("productName", _productName); request.AddHeader("productVersion", _productVersion); request.AddParameter("JWT", jwt); restResponse = _restClient.Execute(request); Response.HttpStatus = restResponse.StatusCode; if (Response.HttpStatus != HttpStatusCode.OK) { var JsonReturn = new JavaScriptSerializer().Deserialize <JsonContent>(restResponse.Content); Response.Severity = JsonReturn.Severity; Response.Message = JsonReturn.Message; Response.Code = JsonReturn.Code; Response.Content = restResponse.Content; return(Response); } else { Response.Severity = string.Empty; Response.Message = string.Empty; Response.Code = string.Empty; Response.Content = restResponse.Content; return(Response); } }