Skip to content

Commit 9d47694

Browse files
Change try catch scope
1 parent 63824cc commit 9d47694

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationHandler.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,39 +84,38 @@ public static string ExtractToken(HttpRequest request)
8484
/// <returns></returns>
8585
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
8686
{
87+
string token;
8788
try
8889
{
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+
}
96101

97-
var validationParameters = Options.TokenValidationParameters.Clone();
102+
var validationParameters = Options.TokenValidationParameters.Clone();
98103

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))
101108
{
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);
108112
}
113+
}
109114

110115
// Ugly patch to make this method should to be async in order to allow result caching by caller
111116
await DoneTask;
112117

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.");
120119
}
121120
}
122121
}

0 commit comments

Comments
 (0)