1
- # JWT RSA C# library
1
+ # JWT C# library
2
2
3
3
A C# class that can sign and validate JWT tokens, wrapped in a simple library with a couple of helper functions.
4
4
5
+ ## RSA Algorithm
6
+
5
7
To generate a compatible private key
6
8
```
7
9
openssl genrsa -out private.key 4096
@@ -12,7 +14,7 @@ To generate a compatible public key
12
14
openssl rsa -in private.key -outform PEM -pubout -out public.pem
13
15
```
14
16
15
- ## Sign a JWT token
17
+ ### Sign a JWT token
16
18
``` cs
17
19
using Newtonsoft .Json ;
18
20
@@ -27,13 +29,13 @@ string signedToken = jwt.Sign(strToken);
27
29
```
28
30
In case of an error, an Exception will be thrown.
29
31
30
- ## Validate a JWT token
32
+ ### Validate a JWT token
31
33
``` cs
32
34
using Newtonsoft .Json ;
33
35
34
36
JwtManager .RsJwt jwt = new JwtManager .RsJwt
35
37
{
36
- KeySize = JwtManager .Helpers .KeySize .S256 , // This can be also 384 or 512
38
+ KeySize = JwtManager .Helpers .KeySize .S256 , // This can be also S384 or S512
37
39
PublicKey = PublicKey
38
40
};
39
41
@@ -43,6 +45,48 @@ var myToken = JsonConvert.DeserializeObject<JwtToken>(payload);
43
45
44
46
In case of an error, an Exception will be thrown.
45
47
48
+
49
+ ## HMAC Algorithm
50
+
51
+ For this you need a secret in store it in a string variable. Use a longer secret for better security
52
+
53
+ ### Sign a JWT token
54
+ ``` cs
55
+ using Newtonsoft .Json ;
56
+
57
+ string secret = " setyourverysecretkeyhere" ;
58
+
59
+ JwtManager .HsJwt jwt = new JwtManager .HsJwt
60
+ {
61
+ KeySize = JwtManager .Helpers .KeySize .S256 , // This can be also 384 or 512
62
+ Secret = secret
63
+ };
64
+
65
+ string strToken = JsonConvert .SerializeObject (myToken );
66
+ string signedToken = jwt .Sign (strToken );
67
+ ```
68
+ In case of an error, an Exception will be thrown.
69
+
70
+ ### Validate a JWT token
71
+ ``` cs
72
+ using Newtonsoft .Json ;
73
+
74
+ string secret = " setyourverysecretkeyhere" ;
75
+
76
+ JwtManager .HsJwt jwt = new JwtManager .HsJwt
77
+ {
78
+ KeySize = JwtManager .Helpers .KeySize .S256 , // This can be also S384 or S512
79
+ Secret = secret
80
+ };
81
+
82
+ string payload = jwt .Validate (strToken );
83
+ var myToken = JsonConvert .DeserializeObject <JwtToken >(payload );
84
+ ```
85
+
86
+ In case of an error, an Exception will be thrown.
87
+
88
+ ## Other Info
89
+
46
90
The code has been tested both as a .NET and .NET Core library.
47
91
48
92
Check the JwtManagerTests project on more examples on how to use
0 commit comments