blowdart

Jwt signing madness

Sep 1st, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.IdentityModel.Protocols.WSTrust;
  3. using System.IdentityModel.Tokens;
  4. using System.Security.Claims;
  5. using System.Security.Cryptography.X509Certificates;
  6.  
  7. namespace JwtValidator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var signingCertificate = new X509Certificate2("JWTSigningCert.pfx", "password");
  14.             var publicCertificate = new X509Certificate2("JWTSigningCert.cer");
  15.             var signedJwt = GenerateSignedJwt(signingCertificate);
  16.             var validatedPrincipal = ValidateTokenWithX509SecurityToken(signedJwt, publicCertificate);
  17.             Console.WriteLine(validatedPrincipal.Identity.Name);
  18.             Console.ReadLine();
  19.         }
  20.  
  21.         static string GenerateSignedJwt(X509Certificate2 signingCertificate)
  22.         {
  23.             var now = DateTime.UtcNow;
  24.             var tokenHandler = new JwtSecurityTokenHandler();
  25.             var tokenDescriptor = new SecurityTokenDescriptor
  26.             {
  27.                
  28.                 Subject = new ClaimsIdentity(new Claim[]
  29.                         {
  30.                         new Claim(ClaimTypes.Name, "John Doe"),
  31.                         }),
  32.                 TokenIssuerName = "self",
  33.                 AppliesToAddress = "http://www.contoso.com",
  34.                 Lifetime = new Lifetime(now, now.AddMinutes(10)),
  35.                 SigningCredentials = new X509SigningCredentials(
  36.                     signingCertificate)
  37.             };
  38.  
  39.             SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
  40.             string tokenString = tokenHandler.WriteToken(token);
  41.  
  42.             return tokenString;
  43.         }
  44.  
  45.         static ClaimsPrincipal ValidateTokenWithX509SecurityToken(string token, X509Certificate2 validatingCertificate)
  46.         {
  47.             SecurityToken validatedToken;
  48.  
  49.             var tokenHandler = new JwtSecurityTokenHandler();
  50.             var validationParameters = new TokenValidationParameters()
  51.             {                
  52.                 ValidateIssuerSigningKey = true,
  53.                 ValidateAudience = true,
  54.                 ValidateIssuer = true,
  55.                 IssuerSigningToken = new X509SecurityToken(validatingCertificate),
  56.                 ValidAudience = "http://www.contoso.com",
  57.                 ValidIssuer = "self",
  58.             };
  59.  
  60.             ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(token, validationParameters, out validatedToken);
  61.  
  62.             return claimsPrincipal;
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment