Skip to content

Commit fdd73a2

Browse files
committed
Updated for new IdentityModel recommendations
1 parent 0009caa commit fdd73a2

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

RsaPssJwtSigning/Program.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IdentityModel.Tokens.Jwt;
43
using System.Security.Claims;
54
using System.Security.Cryptography;
5+
using Microsoft.IdentityModel.JsonWebTokens;
66
using Microsoft.IdentityModel.Tokens;
77
using Xunit;
88

99
namespace ScottBrady91.BlogExampleCode.RsaPssJwtSigning
1010
{
1111
public class Program
1212
{
13-
private static readonly JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
13+
private static readonly JsonWebTokenHandler handler = new JsonWebTokenHandler();
1414
private static readonly RsaSecurityKey key = new RsaSecurityKey(RSA.Create(2048));
1515
private static readonly DateTime now = DateTime.UtcNow;
1616

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

3535
private static string CreatePssToken()
3636
{
37-
var jwt = handler.CreateEncodedJwt(descriptor);
37+
var jwt = handler.CreateToken(descriptor);
3838
Console.WriteLine(jwt);
3939

4040
return jwt;
4141
}
4242

4343
private static void ValidatePssToken(string jwt)
4444
{
45-
var claimsPrincipal = handler.ValidateToken(
46-
jwt,
45+
var result = handler.ValidateToken(jwt,
4746
new TokenValidationParameters
4847
{
4948
ValidIssuer = descriptor.Issuer, // "me"
5049
ValidAudience = descriptor.Audience, // "you"
5150
IssuerSigningKey = new RsaSecurityKey(key.Rsa.ExportParameters(false)) // public key
52-
},
53-
out SecurityToken parsedToken);
51+
});
5452

55-
if (!claimsPrincipal.Identity.IsAuthenticated) throw new Exception("It's all gone wrong");
53+
if (!result.IsValid) throw new Exception("It's all gone wrong");
5654
Console.WriteLine("Token Validated!");
5755
}
5856

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

64-
var token1 = handler.CreateEncodedJwt(descriptor);
65-
var token2 = handler.CreateEncodedJwt(descriptor);
62+
var token1 = handler.CreateToken(descriptor);
63+
var token2 = handler.CreateToken(descriptor);
6664

6765
Assert.Equal(token1, token2);
6866
}
@@ -72,8 +70,8 @@ public void WhenGeneratedWithProbabilisticSignatureScheme_ExpectDifferentJwts()
7270
{
7371
descriptor.SigningCredentials = new SigningCredentials(key, "PS256");
7472

75-
var token1 = handler.CreateEncodedJwt(descriptor);
76-
var token2 = handler.CreateEncodedJwt(descriptor);
73+
var token1 = handler.CreateToken(descriptor);
74+
var token2 = handler.CreateToken(descriptor);
7775

7876
Assert.NotEqual(token1, token2);
7977
}

RsaPssJwtSigning/ScottBrady91.BlogExampleCode.RsaPssJwtSigning.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.5.0" />
10+
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="5.5.0" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1212
<PackageReference Include="xunit" Version="2.4.1" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />

0 commit comments

Comments
 (0)