@@ -84,39 +84,38 @@ public static string ExtractToken(HttpRequest request)
84
84
/// <returns></returns>
85
85
protected override async Task < AuthenticateResult > HandleAuthenticateAsync ( )
86
86
{
87
+ string token ;
87
88
try
88
89
{
89
- var token = ExtractToken ( Request ) ;
90
-
91
- // If no token found, no further work possible
92
- if ( string . IsNullOrEmpty ( token ) )
93
- {
94
- return AuthenticateResult . NoResult ( ) ;
95
- }
90
+ token = ExtractToken ( Request ) ;
91
+ }
92
+ catch ( AuthenticationException ex )
93
+ {
94
+ return AuthenticateResult . Fail ( ex . Message ) ;
95
+ }
96
+ // If no token found, no further work possible
97
+ if ( string . IsNullOrEmpty ( token ) )
98
+ {
99
+ return AuthenticateResult . NoResult ( ) ;
100
+ }
96
101
97
- var validationParameters = Options . TokenValidationParameters . Clone ( ) ;
102
+ var validationParameters = Options . TokenValidationParameters . Clone ( ) ;
98
103
99
- var validators = Options . SecurityTokenValidatorsFactory ( ) ;
100
- foreach ( var validator in validators )
104
+ var validators = Options . SecurityTokenValidatorsFactory ( ) ;
105
+ foreach ( var validator in validators )
106
+ {
107
+ if ( validator . CanReadToken ( token ) )
101
108
{
102
- if ( validator . CanReadToken ( token ) )
103
- {
104
- var principal = validator . ValidateToken ( token , validationParameters , out SecurityToken validatedToken ) ;
105
- var ticket = new AuthenticationTicket ( principal , Scheme . Name ) ;
106
- return AuthenticateResult . Success ( ticket ) ;
107
- }
109
+ var principal = validator . ValidateToken ( token , validationParameters , out SecurityToken validatedToken ) ;
110
+ var ticket = new AuthenticationTicket ( principal , Scheme . Name ) ;
111
+ return AuthenticateResult . Success ( ticket ) ;
108
112
}
113
+ }
109
114
110
115
// Ugly patch to make this method should to be async in order to allow result caching by caller
111
116
await DoneTask ;
112
117
113
- // Not so nice, but AuthenticateResult.Fail does not allow us to show the error
114
- throw new AuthenticationException ( "Authorization token has been detected but it cannot be read." ) ;
115
- }
116
- catch ( AuthenticationException ex )
117
- {
118
- return AuthenticateResult . Fail ( ex . Message ) ;
119
- }
118
+ return AuthenticateResult . Fail ( "Authorization token has been detected but it cannot be read." ) ;
120
119
}
121
120
}
122
121
}
0 commit comments