Beispiel #1
0
        /// <summary>
        /// Method called when a new message is in.
        /// </summary>
        /// <param name="sender">The object which sent the message.</param>
        /// <param name="e">The arguments containing the message.</param>
        private static void IsAuthenticatedOnReceived(object sender, BasicDeliverEventArgs e)
        {
            var body  = e.Body;
            var token = Encoding.UTF8.GetString(body);


            Log.Information("Checking authentication for token \"{Token}\"", token);

            Service service;

            // Check if it exists in the database.
            using (var context = new MonitorContext())
            {
                service = context.Services.FirstOrDefault(x => x.Token == token);
            }

            var isAuthenticatedMessage = new IsAuthenticatedMessage
            {
                CorrelationId = e.BasicProperties.MessageId, IsAuthenticated = service != null, Service = service
            };

            // Send back a JSON message with the authentication status.
            var json = JsonConvert.SerializeObject(isAuthenticatedMessage);

            _authProducer.SendMessage(json);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Test Client");
            Console.WriteLine("Waiting for services to start...\nPress enter when ready.");
            Console.ReadLine();

            authProducer = RabbitMqProducer.Create(StaticQueues.RequestAuth);

            authReceiver = RabbitMqReceiver.Create(APPLICATIONID, OnAuthReceived);

            var service = new Service {
                ApplicationId = APPLICATIONID, GroupId = GROUPID
            };

            var json = JsonConvert.SerializeObject(service);

            var encrypt = EncryptProvider.AESEncrypt(json, KEY, IV);

            authProducer.SendMessage(encrypt);
            Console.WriteLine("Waiting for response.");
        }
        /// <summary>
        /// Checks the cache if the token is already in memory and sends the message back.
        /// </summary>
        /// <param name="message">The message received.</param>
        /// <param name="token">The token received.</param>
        public void CheckAuthentication(string message, string token, bool useAuthentication = true)
        {
            if (!useAuthentication)
            {
                LogMessage(message);
                return;
            }

            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (_authenticatedTokens.ContainsKey(token))
            {
                LogMessage(message);
                return;
            }


            var messageId = _authSender.SendMessage(token);

            _unprocessed.Add(messageId, message);
        }