02-04-21 12:31 AM
using System; using System.IO; using System.Security.Cryptography; using System.IdentityModel.Tokens.Jwt; using Microsoft.IdentityModel.Tokens; using System.Security.Claims; namespace AaaSDocumentation { class SignToken { static void Main(string[] args) { try { // reading the content of a private key PEM file, PKCS8 encoded string privateKeyPem = File.ReadAllText("..."); // keeping only the payload of the key privateKeyPem = privateKeyPem.Replace("-----BEGIN PRIVATE KEY-----", ""); privateKeyPem = privateKeyPem.Replace("-----END PRIVATE KEY-----", ""); byte[] privateKeyRaw = Convert.FromBase64String(privateKeyPem); // creating the RSA key RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.ImportPkcs8PrivateKey(new ReadOnlySpan<byte>(privateKeyRaw), out _); RsaSecurityKey rsaSecurityKey = new RsaSecurityKey(provider); // Generating the token var now = DateTime.UtcNow; var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, "YOUR_CLIENTID"), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; var handler = new JwtSecurityTokenHandler(); var token = new JwtSecurityToken ( "YOUR_CLIENTID", "https://AAAS_PLATFORM/idp/YOUR_TENANT/authn/token", claims, now.AddMilliseconds(-30), now.AddMinutes(60), new SigningCredentials(rsaSecurityKey, SecurityAlgorithms.RsaSha256) ); // handler.WriteToken(token) returns the token ready to send to AaaS ! Console.WriteLine( handler.WriteToken(token) ); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine( new System.Diagnostics.StackTrace().ToString() ); } } } }
02-04-21 05:15 AM
03-04-21 09:53 PM
05-04-21 11:43 AM
Hi,
The function you are trying to use is compatible with .net 5.0+ that is why you are getting the mentioned error.
05-04-21 01:53 PM
05-04-21 02:05 PM
Hi Sheela,
You can try the GitHub - jwt-dotnet/jwt: Jwt.Net, a JWT (JSON Web Token) implementation for .NET for creating a token.
17-05-21 06:55 AM