Example #1
0
        private string EncryptPayload(ApplicationCredentials decryptedAppCredentials)
        {
            var jsonSerializedObject = JsonConvert.SerializeObject(decryptedAppCredentials);
            var encryptedPayload     = _encryptionService.Encrypt(jsonSerializedObject);

            return(Convert.ToBase64String(encryptedPayload));
        }
Example #2
0
        public async Task <AuthenticationResult> ValidateToken(string inboundToken)
        {
            try
            {
                _logger.LogTrace("ApplicationAuthService::ValidatingToken started");
                var sw2 = new Stopwatch();
                sw2.Start();
                var appCredentials          = new ApplicationCredentials(_appSettings.ApplicationName, _appSettings.Secret);
                var encryptedAppCredentials = EncryptPayload(appCredentials);
                sw2.Stop();
                Debug.WriteLine($"encryptedAppCredentials took: {sw2.ElapsedMilliseconds}");

                var sw = new Stopwatch();
                sw.Start();
                var result = await _authorizationHttpClient.ValidateToken(encryptedAppCredentials, inboundToken);

                sw.Stop();
                Debug.WriteLine($"ValidateToken call took: {sw.ElapsedMilliseconds}");

                if (result != AuthenticationResult.Success)
                {
                    _logger.LogWarning($"Attempt to authenticate {appCredentials?.Name} resulted in {result}.");
                }

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An error occurred while authenticating.");
                return(AuthenticationResult.Unknown);
            }
        }