Exemple #1
0
        static void Main(string[] args)
        {
            MSMQHelper msmqHelper           = new MSMQHelper();
            string     queueName            = Environment.GetEnvironmentVariable("QUEUE_NAME") ?? Constants.PRIVATE_QUEUE_NAME;
            string     directFormatProtocol = Environment.GetEnvironmentVariable("DIRECT_FORMAT_PROTOCOL") ?? Constants.DIRECT_FORMAT_PROTOCOL;

            Console.WriteLine("This should run as a separate user from the sending app.\r\nThis will try to receive a message.");
            while (true)
            {
                try
                {
                    Message msg = msmqHelper.ReceiveMessage(queueName, directFormatProtocol);
                    Console.WriteLine(Constants.TRACE_LINE_BREAK);
                    Console.WriteLine(String.Format("Received a message {0}", msg.Body));
                    Console.WriteLine(Constants.TRACE_LINE_BREAK);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine("This will try to receive a message.");

                //Console.ReadLine();
                Thread.Sleep(1000);
            }
        }
Exemple #2
0
        static void RequestJWTToken()
        {
            Console.WriteLine("Username:"******"Sending request to token system");
            MSMQHelper.SendMessage(userInputMQ, userModel, TokenRequestType.CreateToken.ToString(), testMQ);

            Console.WriteLine("Awaiting response in testMQ");
            Message msg = MSMQHelper.ReceiveMessage(testMQ);

            //JWTPayload payload = JWTManager.GetModelFromToken<JWTPayload>(msg.Body);


            JWTPayload payload = MSMQHelper.GetMessageBody <JWTPayload>(msg);


            Console.WriteLine("Response received");
            Console.WriteLine("Printing payload:");
            Console.WriteLine("UserID = " + payload.UserID);
            Console.WriteLine("Servers:");
            foreach (var item in payload.ServersInfo.Servers)
            {
                Console.WriteLine(item.IP + ":" + item.Port);
            }
        }
Exemple #3
0
 static void Main(string[] args)
 {
     MSMQHelper.SetClientQueuePath("FormatName:Direct=TCP:192.168.1.4\\private$\\Test");
     while (true)
     {
         for (int i = 0; i < 10; i++)
         {
             MSMQHelper.ReceiveMessage(false);
             Thread.Sleep(100);
         }
         Thread.Sleep(1000);
     }
 }
Exemple #4
0
        private static void UserInputRecieved(object sender, ReceiveCompletedEventArgs e)
        {
            MessageQueue mQ = (MessageQueue)sender;
            Message      m  = mQ.EndReceive(e.AsyncResult);

            try
            {
                switch (Enum.Parse(typeof(TokenRequestType), m.Label))
                {
                case TokenRequestType.CreateToken:
                {
                    //UserModel originModel = MSMQHelper.GetMessageBody<UserModel>(m);
                    UserModel userModel = MSMQHelper.GetMessageBody <UserModel>(m);
                    Console.WriteLine("UserModel received!");

                    MSMQHelper.SendMessage(beaconInputMQ, "ServersData", "ServersData", beaconResponseMQ);

                    ServersData data = MSMQHelper.GetMessageBody <ServersData>(MSMQHelper.ReceiveMessage(beaconResponseMQ, new TimeSpan(0, 0, 5)));

                    JWTPayload payload = new JWTPayload()
                    {
                        UserID = userModel.UserID, ServersInfo = data
                    };

                    userModel.Token         = JWTManager.CreateJWT(JWTManager.CreateClaims <JWTPayload>(payload), 5).RawData;
                    userModel.TokenResponse = TokenResponse.Created;

                    Message userResponse = new Message()
                    {
                        Formatter = new JsonMessageFormatter(),
                        Body      = JsonConvert.SerializeObject(userModel),
                        Label     = userModel.UserID
                    };

                    MSMQHelper.SendMessage(m.ResponseQueue, userResponse);
                    Console.WriteLine("Token send to {0}", m.ResponseQueue.Path);
                    break;
                }

                case TokenRequestType.VerifyToken:
                {
                    UserModel userModel = MSMQHelper.GetMessageBody <UserModel>(m);

                    if (JWTManager.VerifyToken(userModel.Token))
                    {
                        userModel.TokenResponse = TokenResponse.Valid;
                        userModel.Message       = "Token Valid, Connecting to Server!";
                        Console.WriteLine("\n=======TOKEN======");
                        Console.WriteLine(userModel.Token);
                        Console.WriteLine("=======TOKEN======\n");

                        Message userResponse = new Message()
                        {
                            Formatter = new JsonMessageFormatter(),
                            Body      = JsonConvert.SerializeObject(userModel),
                            Label     = userModel.UserID
                        };
                        MSMQHelper.SendMessage(m.ResponseQueue, userResponse);
                        Console.WriteLine("Token was valid!");
                        Console.WriteLine("Response send to {0}", m.ResponseQueue.Path);
                    }
                    else
                    {
                        userModel.TokenResponse = TokenResponse.Invalid;
                        userModel.Message       = "Session Token no longer valid!\n Please login, using credentials.";

                        Message userResponse = new Message()
                        {
                            Formatter = new JsonMessageFormatter(),
                            Body      = JsonConvert.SerializeObject(userModel),
                            Label     = userModel.UserID
                        };

                        MSMQHelper.SendMessage(m.ResponseQueue, userResponse);
                        Console.WriteLine("Token was invalid!");
                        Console.WriteLine("Response send to {0}", m.ResponseQueue.Path);
                    }
                    break;
                }
                }
            }
            catch (Exception error)
            {
                Console.WriteLine("Couldn't read message!");
                Console.WriteLine(error);
                Console.WriteLine(error.Message);

                UserModel userModel = new UserModel()
                {
                    UserID        = JsonConvert.DeserializeObject <UserModel>(m.Body.ToString()).UserID,
                    RequestType   = RequestTypes.Error,
                    TokenResponse = TokenResponse.Invalid,
                    Message       = "Token Request Failed: " + error.Message
                };

                Message userResponse = new Message()
                {
                    Formatter = new JsonMessageFormatter(),
                    Body      = JsonConvert.SerializeObject(userModel),
                    Label     = userModel.UserID
                };

                MSMQHelper.SendMessage(m.ResponseQueue, userResponse);
            }

            mQ.BeginReceive();
        }