public static string Encode(Dictionary <string, object> payload, string secret, JwtHashAlgorithm alg) { IJwtAlgorithm algorithm; switch (alg) { case JwtHashAlgorithm.HS256: algorithm = new HMACSHA256Algorithm(); break; case JwtHashAlgorithm.HS384: algorithm = new HMACSHA384Algorithm(); break; case JwtHashAlgorithm.HS512: algorithm = new HMACSHA512Algorithm(); break; default: algorithm = new HMACSHA256Algorithm(); break; } IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); return(encoder.Encode(payload, secret)); }
private void metroButtonGenerateJey_Click(object sender, EventArgs e) { try { if (metroTextBoxEncryptionKey.Text != string.Empty && metroTextBoxEncryptionKey.Enabled == true) { encryptionkey = metroTextBoxEncryptionKey.Text.ToString(); } else if (metroCheckBoxUseDefault.Checked) { encryptionkey = "KwkQ37eYtFJ94mpsuoWuyVph5vLpDmeX9FYFsSLqsUTzMvyeW2dZcN7PW2eQKJzQEDJ9JDL3LpKki9eDtDkDDHgiyroMNb7zcfysdXat"; } else { encryptionkey = null; } IJwtAlgorithm algorithm; if (metroComboBoxHashingAlgorithm.SelectedItem.ToString() == "HS256") { algorithm = new HMACSHA256Algorithm(); } else if (metroComboBoxHashingAlgorithm.SelectedItem.ToString() == "HS384") { algorithm = new HMACSHA384Algorithm(); } else if (metroComboBoxHashingAlgorithm.SelectedItem.ToString() == "HS512") { algorithm = new HMACSHA512Algorithm(); } else if (metroComboBoxHashingAlgorithm.SelectedItem.ToString() == "RS256") { algorithm = new HMACSHA256Algorithm(); } else { algorithm = new HMACSHA256Algorithm(); } var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(encryptionkey)); var secToken = new JwtSecurityToken( signingCredentials: new SigningCredentials(securityKey, metroComboBoxHashingAlgorithm.SelectedItem.ToString()), issuer: "JWT Manager (https://github.com/sajeebchandan/JWTManager)", audience: "JWT Manager (https://github.com/sajeebchandan/JWTManager)", claims: payload, expires: DateTime.UtcNow.AddDays(30)); var handler = new JwtSecurityTokenHandler(); JWT _JWT = new JWT(handler.WriteToken(secToken)); _JWT.ShowDialog(); } catch (Exception ex) { MetroFramework.MetroMessageBox.Show(this, "One or more field required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }