@@ -26,6 +26,31 @@ public static class Authentication
2626 return context . Response . WriteAsync ( JsonSerializer . Serialize ( AuthenticationErrors . InvalidTokenFormat , _serializer ) ) ;
2727 } ,
2828
29+ OnTokenValidated = context =>
30+ {
31+ var request = context . HttpContext . Request ;
32+ if ( context . SecurityToken is not Microsoft . IdentityModel . JsonWebTokens . JsonWebToken token )
33+ {
34+ context . HttpContext . Items [ "authentication.error" ] = AuthenticationErrors . InvalidTokenFormat ;
35+ context . Fail ( "The token format is invalid or the token is malformed." ) ;
36+
37+ return Task . CompletedTask ;
38+ }
39+
40+ var expectedIssuer = $ "{ request . Scheme } ://{ request . Host } ". TrimEnd ( '/' ) ;
41+ var actualIssuer = token . Issuer ? . TrimEnd ( '/' ) ;
42+
43+ if ( ! string . Equals ( actualIssuer , expectedIssuer , StringComparison . OrdinalIgnoreCase ) )
44+ {
45+ context . HttpContext . Items [ "authentication.error" ] = AuthenticationErrors . InvalidIssuer ;
46+ context . Fail ( "The token issuer is invalid." ) ;
47+
48+ return Task . CompletedTask ;
49+ }
50+
51+ return Task . CompletedTask ;
52+ } ,
53+
2954 OnChallenge = context =>
3055 {
3156 context . HandleResponse ( ) ;
@@ -36,7 +61,11 @@ public static class Authentication
3661 context . Response . StatusCode = StatusCodes . Status401Unauthorized ;
3762 context . Response . ContentType = MediaTypeNames . Application . Json ;
3863
39- return context . Response . WriteAsync ( JsonSerializer . Serialize ( AuthenticationErrors . Unauthenticated , _serializer ) ) ;
64+ var error = context . HttpContext . Items [ "authentication.error" ] as Error
65+ ?? AuthenticationErrors . Unauthenticated ;
66+
67+ return context . Response . WriteAsync ( JsonSerializer . Serialize ( error , _serializer ) ) ;
4068 }
4169 } ;
42- }
70+ }
71+
0 commit comments