1
+ using System ;
2
+ using System . Threading . Tasks ;
3
+ using Microsoft . IdentityModel . Tokens ;
4
+
5
+ namespace ApiDemo
6
+ {
7
+ public class JwtIssuerOptions
8
+ {
9
+ /// <summary>
10
+ /// "iss" (Issuer) Claim
11
+ /// </summary>
12
+ /// <remarks>The "iss" (issuer) claim identifies the principal that issued the
13
+ /// JWT. The processing of this claim is generally application specific.
14
+ /// The "iss" value is a case-sensitive string containing a StringOrURI
15
+ /// value. Use of this claim is OPTIONAL.</remarks>
16
+ public string Issuer { get ; set ; }
17
+
18
+ /// <summary>
19
+ /// "sub" (Subject) Claim
20
+ /// </summary>
21
+ /// <remarks> The "sub" (subject) claim identifies the principal that is the
22
+ /// subject of the JWT. The claims in a JWT are normally statements
23
+ /// about the subject. The subject value MUST either be scoped to be
24
+ /// locally unique in the context of the issuer or be globally unique.
25
+ /// The processing of this claim is generally application specific. The
26
+ /// "sub" value is a case-sensitive string containing a StringOrURI
27
+ /// value. Use of this claim is OPTIONAL.</remarks>
28
+ public string Subject { get ; set ; }
29
+
30
+ /// <summary>
31
+ /// "aud" (Audience) Claim
32
+ /// </summary>
33
+ /// <remarks>The "aud" (audience) claim identifies the recipients that the JWT is
34
+ /// intended for. Each principal intended to process the JWT MUST
35
+ /// identify itself with a value in the audience claim. If the principal
36
+ /// processing the claim does not identify itself with a value in the
37
+ /// "aud" claim when this claim is present, then the JWT MUST be
38
+ /// rejected. In the general case, the "aud" value is an array of case-
39
+ /// sensitive strings, each containing a StringOrURI value. In the
40
+ /// special case when the JWT has one audience, the "aud" value MAY be a
41
+ /// single case-sensitive string containing a StringOrURI value. The
42
+ /// interpretation of audience values is generally application specific.
43
+ /// Use of this claim is OPTIONAL.</remarks>
44
+ public string Audience { get ; set ; }
45
+
46
+ /// <summary>
47
+ /// "nbf" (Not Before) Claim (default is UTC NOW)
48
+ /// </summary>
49
+ /// <remarks>The "nbf" (not before) claim identifies the time before which the JWT
50
+ /// MUST NOT be accepted for processing. The processing of the "nbf"
51
+ /// claim requires that the current date/time MUST be after or equal to
52
+ /// the not-before date/time listed in the "nbf" claim. Implementers MAY
53
+ /// provide for some small leeway, usually no more than a few minutes, to
54
+ /// account for clock skew. Its value MUST be a number containing a
55
+ /// NumericDate value. Use of this claim is OPTIONAL.</remarks>
56
+ public DateTime NotBefore => DateTime . UtcNow ;
57
+
58
+ /// <summary>
59
+ /// "iat" (Issued At) Claim (default is UTC NOW)
60
+ /// </summary>
61
+ /// <remarks>The "iat" (issued at) claim identifies the time at which the JWT was
62
+ /// issued. This claim can be used to determine the age of the JWT. Its
63
+ /// value MUST be a number containing a NumericDate value. Use of this
64
+ /// claim is OPTIONAL.</remarks>
65
+ public DateTime IssuedAt => DateTime . UtcNow ;
66
+
67
+ /// <summary>
68
+ /// Set the timespan the token will be valid for (default is 5 min/300 seconds)
69
+ /// </summary>
70
+ public TimeSpan ValidFor { get ; set ; } = TimeSpan . FromMinutes ( 5 ) ;
71
+
72
+ /// <summary>
73
+ /// "exp" (Expiration Time) Claim (returns IssuedAt + ValidFor)
74
+ /// </summary>
75
+ /// <remarks>The "exp" (expiration time) claim identifies the expiration time on
76
+ /// or after which the JWT MUST NOT be accepted for processing. The
77
+ /// processing of the "exp" claim requires that the current date/time
78
+ /// MUST be before the expiration date/time listed in the "exp" claim.
79
+ /// Implementers MAY provide for some small leeway, usually no more than
80
+ /// a few minutes, to account for clock skew. Its value MUST be a number
81
+ /// containing a NumericDate value. Use of this claim is OPTIONAL.</remarks>
82
+ public DateTime Expiration => IssuedAt . Add ( ValidFor ) ;
83
+
84
+ /// <summary>
85
+ /// "jti" (JWT ID) Claim (default ID is a GUID)
86
+ /// </summary>
87
+ /// <remarks>The "jti" (JWT ID) claim provides a unique identifier for the JWT.
88
+ /// The identifier value MUST be assigned in a manner that ensures that
89
+ /// there is a negligible probability that the same value will be
90
+ /// accidentally assigned to a different data object; if the application
91
+ /// uses multiple issuers, collisions MUST be prevented among values
92
+ /// produced by different issuers as well. The "jti" claim can be used
93
+ /// to prevent the JWT from being replayed. The "jti" value is a case-
94
+ /// sensitive string. Use of this claim is OPTIONAL.</remarks>
95
+ public Func < Task < string > > JtiGenerator =>
96
+ ( ) => Task . FromResult ( Guid . NewGuid ( ) . ToString ( ) ) ;
97
+
98
+ /// <summary>
99
+ /// The signing key to use when generating tokens.
100
+ /// </summary>
101
+ public SigningCredentials SigningCredentials { get ; set ; }
102
+ }
103
+ }
0 commit comments