Skip to content

Commit 59ab8c7

Browse files
committed
Added JWE example code
1 parent dcf599c commit 59ab8c7

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.271
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScottBrady91.BlogExampleCode.Jwe", "ScottBrady91.BlogExampleCode.Jwe\ScottBrady91.BlogExampleCode.Jwe.csproj", "{548770CC-9A5C-4570-9320-04DF084AF5BE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{548770CC-9A5C-4570-9320-04DF084AF5BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{548770CC-9A5C-4570-9320-04DF084AF5BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{548770CC-9A5C-4570-9320-04DF084AF5BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{548770CC-9A5C-4570-9320-04DF084AF5BE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {5F02898E-C54F-42FB-AE07-6E720D67F034}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IdentityModel.Tokens.Jwt;
4+
using System.Security.Claims;
5+
using System.Security.Cryptography.X509Certificates;
6+
using Microsoft.IdentityModel.Tokens;
7+
8+
namespace ScottBrady91.BlogExampleCode.Jwe
9+
{
10+
public class Program
11+
{
12+
public static void Main()
13+
{
14+
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
15+
var handler = new JwtSecurityTokenHandler();
16+
17+
var tokenDescriptor = new SecurityTokenDescriptor
18+
{
19+
Audience = "you",
20+
Issuer = "me",
21+
Subject = new ClaimsIdentity(new List<Claim>{new Claim("sub", "scott")}),
22+
EncryptingCredentials = new X509EncryptingCredentials(new X509Certificate2("key_public.cer"))
23+
};
24+
25+
var token = handler.CreateEncodedJwt(tokenDescriptor);
26+
27+
Console.WriteLine("JWE:");
28+
Console.WriteLine(token + "\n");
29+
30+
Console.WriteLine("Press enter to validate token...");
31+
Console.ReadLine();
32+
33+
var claimsPrincipal = handler.ValidateToken(
34+
token,
35+
new TokenValidationParameters
36+
{
37+
ValidAudience = "you",
38+
ValidIssuer = "me",
39+
RequireSignedTokens = false,
40+
TokenDecryptionKey = new X509SecurityKey(new X509Certificate2("key_private.pfx", "idsrv3test"))
41+
},
42+
out SecurityToken securityToken);
43+
44+
Console.WriteLine("Token validated and read for user: " + claimsPrincipal.FindFirst("sub").Value);
45+
Console.ReadLine();
46+
}
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.3.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<None Update="key_private.pfx">
14+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15+
</None>
16+
<None Update="key_public.cer">
17+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Binary file not shown.
777 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)