-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Wrapping the exception to provide better feedback to the user; fixes … #4606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,35 +47,46 @@ private License Initialize() | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private Claim[] ValidateKey(string licenseKey) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| var handler = new JsonWebTokenHandler(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var rsa = new RSAParameters | ||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| Exponent = Convert.FromBase64String("AQAB"), | ||||||||||||||||||||||||||||||
| Modulus = Convert.FromBase64String( | ||||||||||||||||||||||||||||||
| "2LTtdJV2b0mYoRqChRCfcqnbpKvsiCcDYwJ+qPtvQXWXozOhGo02/V0SWMFBdbZHUzpEytIiEcojo7Vbq5mQmt4lg92auyPKsWq6qSmCVZCUuL/kpYqLCit4yUC0YqZfw4H9zLf1yAIOgyXQf1x6g+kscDo1pWAniSl9a9l/LXRVEnGz+OfeUrN/5gzpracGUY6phx6T09UCRuzi4YqqO4VJzL877W0jCW2Q7jMzHxOK04VSjNc22CADuCd34mrFs23R0vVm1DVLYtPGD76/rGOcxO6vmRc7ydBAvt1IoUsrY0vQ2rahp51YPxqqhKPd8nNOomHWblCCA7YUeV3C1Q==") | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| var handler = new JsonWebTokenHandler(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var key = new RsaSecurityKey(rsa) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| KeyId = "LuckyPennySoftwareLicenseKey/bbb13acb59904d89b4cb1c85f088ccf9" | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| var rsa = new RSAParameters | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| Exponent = Convert.FromBase64String("AQAB"), | ||||||||||||||||||||||||||||||
| Modulus = Convert.FromBase64String( | ||||||||||||||||||||||||||||||
| "2LTtdJV2b0mYoRqChRCfcqnbpKvsiCcDYwJ+qPtvQXWXozOhGo02/V0SWMFBdbZHUzpEytIiEcojo7Vbq5mQmt4lg92auyPKsWq6qSmCVZCUuL/kpYqLCit4yUC0YqZfw4H9zLf1yAIOgyXQf1x6g+kscDo1pWAniSl9a9l/LXRVEnGz+OfeUrN/5gzpracGUY6phx6T09UCRuzi4YqqO4VJzL877W0jCW2Q7jMzHxOK04VSjNc22CADuCd34mrFs23R0vVm1DVLYtPGD76/rGOcxO6vmRc7ydBAvt1IoUsrY0vQ2rahp51YPxqqhKPd8nNOomHWblCCA7YUeV3C1Q==") | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var parms = new TokenValidationParameters | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| ValidIssuer = "https://luckypennysoftware.com", | ||||||||||||||||||||||||||||||
| ValidAudience = "LuckyPennySoftware", | ||||||||||||||||||||||||||||||
| IssuerSigningKey = key, | ||||||||||||||||||||||||||||||
| ValidateLifetime = false | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| var key = new RsaSecurityKey(rsa) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| KeyId = "LuckyPennySoftwareLicenseKey/bbb13acb59904d89b4cb1c85f088ccf9" | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var parms = new TokenValidationParameters | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| ValidIssuer = "https://luckypennysoftware.com", | ||||||||||||||||||||||||||||||
| ValidAudience = "LuckyPennySoftware", | ||||||||||||||||||||||||||||||
| IssuerSigningKey = key, | ||||||||||||||||||||||||||||||
| ValidateLifetime = false | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var validateResult = handler.ValidateTokenAsync(licenseKey, parms).Result; | ||||||||||||||||||||||||||||||
| if (!validateResult.IsValid) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| _logger.LogCritical(validateResult.Exception, "Error validating the Lucky Penny software license key"); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var validateResult = handler.ValidateTokenAsync(licenseKey, parms).Result; | ||||||||||||||||||||||||||||||
| if (!validateResult.IsValid) | ||||||||||||||||||||||||||||||
| return validateResult.ClaimsIdentity?.Claims.ToArray() ?? []; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| catch (PlatformNotSupportedException) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| _logger.LogCritical(validateResult.Exception, "Error validating the Lucky Penny software license key"); | ||||||||||||||||||||||||||||||
| _logger.LogInformation( | ||||||||||||||||||||||||||||||
| "RSA cryptography is not supported on this platform. " + | ||||||||||||||||||||||||||||||
| "For client redistribution scenarios such as Blazor WASM, see: " + | ||||||||||||||||||||||||||||||
| "https://docs.automapper.io/en/latest/License-configuration.html#client-redistribution-scenarios"); | ||||||||||||||||||||||||||||||
| return []; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+82
to
89
|
||||||||||||||||||||||||||||||
| } | |
| } | |
| catch (ArgumentException ex) | |
| { | |
| _logger.LogWarning(ex, | |
| "The provided license key is invalid or malformed. Please check that you have copied the entire license key correctly."); | |
| return []; | |
| } | |
| catch (FormatException ex) | |
| { | |
| _logger.LogWarning(ex, | |
| "The provided license key is invalid or malformed. Please check that you have copied the entire license key correctly."); | |
| return []; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using .Result on an async method can cause deadlocks in certain synchronization contexts and wraps exceptions in AggregateException. Since the handler.ValidateTokenAsync call might throw exceptions synchronously (before returning a Task), those exceptions may be wrapped in AggregateException when accessing .Result. This could complicate exception handling. Consider using GetAwaiter().GetResult() instead, which unwraps the exception, or making this method async and using await.