Skip to content

Instantly share code, notes, and snippets.

@joncloud
Last active December 9, 2016 15:31
Show Gist options
  • Select an option

  • Save joncloud/776375079bc24535bfe0b96f45f5da3a to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/776375079bc24535bfe0b96f45f5da3a to your computer and use it in GitHub Desktop.
JWT Perf
BenchmarkDotNet=v0.10.1, OS=Windows
Processor=?, ProcessorCount=8
Frequency=2435775 Hz, Resolution=410.5470 ns, Timer=TSC
dotnet cli version=1.0.0-preview2-003131
  [Host]     : .NET Core 4.6.24628.01, 64bit RyuJIT
  DefaultJob : .NET Core 4.6.24628.01, 64bit RyuJIT
    Method |       Mean |    StdErr |    StdDev |  Gen 0 | Allocated |

-------------- |----------- |---------- |---------- |------- |---------- | CreateToken | 20.2928 us | 0.0291 us | 0.1129 us | 1.9043 | 16.03 kB | ValidateToken | 39.0541 us | 0.4278 us | 1.7640 us | 3.0924 | 28.19 kB |

// * Diagnostic Output - MemoryDiagnoser *
Note: the Gen 0/1/2 Measurements are per 1k Operations
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace ConsoleApp6
{
public class Program
{
private SigningCredentials credentials;
private SymmetricSecurityKey securityKey;
private JwtSecurityTokenHandler tokenHandler;
private TokenValidationParameters tokenValidationParameters;
private string token;
private JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
public static void Main(string[] args)
{
BenchmarkRunner.Run<Program>();
}
[Setup]
public void Setup()
{
byte[] key = Encoding.UTF8.GetBytes("9251E97D-7962-4B49-BB87-96ECADCEE092");
securityKey = new SymmetricSecurityKey(key);
tokenHandler = new JwtSecurityTokenHandler();
credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
tokenValidationParameters = new TokenValidationParameters
{
AuthenticationType = "bearer",
IssuerSigningKey = securityKey,
ValidAudience = "accelamerchantcenter",
ValidIssuer = "accelamerchantcenter"
};
token = CreateToken();
}
[Benchmark]
public string CreateToken()
{
ClaimsIdentity subject = new ClaimsIdentity();
JwtSecurityToken jwtToken = tokenHandler.CreateJwtSecurityToken
(
"accelamerchantcenter",
"accelamerchantcenter",
subject,
DateTime.UtcNow,
DateTime.UtcNow.AddHours(6),
DateTime.UtcNow,
credentials
);
return handler.WriteToken(jwtToken);
}
[Benchmark]
public ClaimsPrincipal ValidateToken()
{
try
{
// Validate the token, returning a generated principal if valid
SecurityToken validatedToken;
return tokenHandler.ValidateToken(token, tokenValidationParameters, out validatedToken);
}
catch (SecurityTokenValidationException)
{
// Handle tokens which failed validation
return null;
}
catch (ArgumentException)
{
// Handle malformed token strings that failed validation
return null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment