Skip to content

Commit

Permalink
Updated for new IdentityModel recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbrady91 committed Jul 30, 2019
1 parent 0009caa commit fdd73a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
22 changes: 10 additions & 12 deletions RsaPssJwtSigning/Program.cs
@@ -1,16 +1,16 @@
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using Xunit;

namespace ScottBrady91.BlogExampleCode.RsaPssJwtSigning
{
public class Program
{
private static readonly JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
private static readonly JsonWebTokenHandler handler = new JsonWebTokenHandler();
private static readonly RsaSecurityKey key = new RsaSecurityKey(RSA.Create(2048));
private static readonly DateTime now = DateTime.UtcNow;

Expand All @@ -34,25 +34,23 @@ public static void Main(string[] args)

private static string CreatePssToken()
{
var jwt = handler.CreateEncodedJwt(descriptor);
var jwt = handler.CreateToken(descriptor);
Console.WriteLine(jwt);

return jwt;
}

private static void ValidatePssToken(string jwt)
{
var claimsPrincipal = handler.ValidateToken(
jwt,
var result = handler.ValidateToken(jwt,
new TokenValidationParameters
{
ValidIssuer = descriptor.Issuer, // "me"
ValidAudience = descriptor.Audience, // "you"
IssuerSigningKey = new RsaSecurityKey(key.Rsa.ExportParameters(false)) // public key
},
out SecurityToken parsedToken);
});

if (!claimsPrincipal.Identity.IsAuthenticated) throw new Exception("It's all gone wrong");
if (!result.IsValid) throw new Exception("It's all gone wrong");
Console.WriteLine("Token Validated!");
}

Expand All @@ -61,8 +59,8 @@ public void WhenGeneratedWithDeterministicSignatureScheme_ExpectIdenticalJwts()
{
descriptor.SigningCredentials = new SigningCredentials(key, "RS256");

var token1 = handler.CreateEncodedJwt(descriptor);
var token2 = handler.CreateEncodedJwt(descriptor);
var token1 = handler.CreateToken(descriptor);
var token2 = handler.CreateToken(descriptor);

Assert.Equal(token1, token2);
}
Expand All @@ -72,8 +70,8 @@ public void WhenGeneratedWithProbabilisticSignatureScheme_ExpectDifferentJwts()
{
descriptor.SigningCredentials = new SigningCredentials(key, "PS256");

var token1 = handler.CreateEncodedJwt(descriptor);
var token2 = handler.CreateEncodedJwt(descriptor);
var token1 = handler.CreateToken(descriptor);
var token2 = handler.CreateToken(descriptor);

Assert.NotEqual(token1, token2);
}
Expand Down
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.5.0" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="5.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
Expand Down

0 comments on commit fdd73a2

Please sign in to comment.