Please see codes below. I got the following error on line
var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
Error: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
Not sure what i did wrong. Please help!
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.IdentityModel.Tokens;
using System.Security.Cryptography;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string privateKeyFilePath = "C:\\Users\\privateKey.txt";
string privateKey;
string clientId = "myclientid";
string serraviewUri = "https://instance.serraview.com";
try
{
privateKey = File.ReadAllText(privateKeyFilePath);
}
catch (Exception ex)
{
return;
}
// Convert PEM to RSA using the BouncyCastle library.
var rsa = new RSACryptoServiceProvider();
RSAParameters rsaParameters;
using (var privateKeyReader = new StringReader(privateKey))
{
var pemReader = new PemReader(privateKeyReader);
var keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
var privKey = (RsaPrivateCrtKeyParameters)keyPair.Private;
rsaParameters = DotNetUtilities.ToRSAParameters(privKey);
}
rsa.ImportParameters(rsaParameters);
// The only signing algorithm supported by the Serraview Authorization Server is RSA using SHA-256 hashing algorithm.
var signingKey = new RsaSecurityKey(rsa);
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest);
// Get audience for the JWT. This will be the url for the authorization server we request an access token from.
Uri uri;
Uri.TryCreate(serraviewUri, UriKind.Absolute, out uri);
Uri audience;
if (!Uri.TryCreate(uri, "/oauth2/token", out audience))
{
return;
}
// Build JWT
var jwt = new JwtSecurityToken(
clientId,
audience.ToString(),
null,
DateTime.UtcNow /*not before*/,
DateTime.UtcNow.AddMinutes(5) /*expires*/,
signingCredentials);
// Serialize our jwt object to an actual JWT string
var jwtString = new JwtSecurityTokenHandler().WriteToken(jwt);
// Request an access token
// Serraview servers require TLS1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
string accessToken;
using (var client = new System.Net.Http.HttpClient())
{
client.BaseAddress = uri;
var content = new System.Net.Http.FormUrlEncodedContent(
new[]
{
new KeyValuePair<string, string>("grant_type","urn:ietf:params:oauth:grant-type:jwt-bearer"),
new KeyValuePair<string, string>("assertion", jwtString)
});
var response = client.PostAsync("/oauth2/token", content).GetAwaiter().GetResult();
var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
dynamic parsedJson = JsonConvert.DeserializeObject(result);
accessToken = parsedJson["access_token"];
}
Console.WriteLine(accessToken);
Console.Read();
}
}
}
I tried webclient and the same issue. The response returns 200 but output "Error: the input...." after encoding it to a string.
The following is part of the SerraView API doc.
I have tested with Postman and it threw "unsupported grant_type"