-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathProgram.cs
97 lines (89 loc) · 3.12 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using JsonWebToken;
namespace JwsCreationSample
{
class Program
{
static void Main()
{
// Creates a symmetric key defined for the 'HS256' algorithm
var signingKey = SymmetricJwk.FromBase64Url("R9MyWaEoyiMYViVWo8Fk4TUGWiSoaW6U1nOqXri8_XU");
// Creates a JWS descriptor with all its properties
var descriptor = new JwsDescriptor(signingKey, SignatureAlgorithm.HS256)
{
Payload = new JwtPayload
{
// You can use predefined claims
{ JwtClaimNames.Iat, EpochTime.UtcNow },
{ JwtClaimNames.Exp, EpochTime.UtcNow + EpochTime.OneHour },
{ JwtClaimNames.Iss, "https://idp.example.com/" },
{ JwtClaimNames.Aud, "636C69656E745F6964" },
// Or use custom claims
{ "value", "ABCEDF" }
}
};
// Adds another claim
descriptor.Payload.Add("ClaimName", new JsonObject
{
{ "stuff1", "xyz789" },
{ "stuff2", "abc123" },
{
"subObject" , new JsonObject
{
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
}
},
{
"Modules" , new []
{
new JsonObject
{
{ "name" , "module1" },
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
},
new JsonObject
{
{ "name" , "module2" },
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
}
}
}
});
// Adds anonymous object
descriptor.Payload.Add("ClaimName_anonymous_type", new
{
stuff1 = "xyz789",
stuff2 = "abc123",
subObject = new
{
prop1 = "abc123",
prop2 = "xyz789"
},
Modules = new[]
{
new {
name = "module1",
prop1 = "abc123",
prop2 = "xyz789"
},
new {
name = "module2",
prop1 = "abc123",
prop2 = "xyz789"
}
}
});
// Generates the UTF-8 string representation of the JWT
var writer = new JwtWriter();
var token = writer.WriteTokenString(descriptor);
Console.WriteLine("The JWT is:");
Console.WriteLine(descriptor);
Console.WriteLine();
Console.WriteLine("Its compact form is:");
Console.WriteLine(token);
}
}
}