//[ActionName("SendMessageExistingConversation")]
        public IHttpActionResult SendMessageWithNewConversation([FromBody] NewConversationMessageDTO newConversationMessageDTO)
        {
            //The authentication part start from here
            //Create security context from the token
            UserManager     um = new UserManager();
            SecurityContext securityContext = SecurityContextBuilder.CreateSecurityContext(Request.Headers);

            if (securityContext == null)
            {
                return(Unauthorized());;
            }

            //Validate the token
            SessionManager sm = new SessionManager();

            if (!sm.ValidateSession(securityContext.Token))
            {
                return(Unauthorized());;
            }

            //Create authorization manager from security context to check claims
            AuthorizationManager authorizationManager = new AuthorizationManager(
                securityContext
                );
            // TODO get this from table in database.
            List <string> requiredClaims = new List <string>()
            {
                "CanSendMessage"
            };

            if (!authorizationManager.CheckClaims(requiredClaims))
            {
                return(Unauthorized());;
            }
            else
            {
                // Find auth user id from auth user name
                _authUserId = um.FindByUserName(securityContext.UserName).Id;
                var authUsername = securityContext.UserName;

                // Find contact user name
                var     contactUser = um.FindByUserName(newConversationMessageDTO.ContactUsername);
                Message returnMessage;

                if (contactUser != null && contactUser.Id != _authUserId)
                {
                    // Map the message to store in database
                    var message = new Message
                    {
                        MessageContent  = newConversationMessageDTO.MessageContent,
                        OutgoingMessage = true,
                        CreatedDate     = DateTime.Now
                    };

                    // Save message to database
                    try
                    {
                        returnMessage = _messengerManager.SaveMessageToDatabase(message, _authUserId, contactUser.Id);
                    }

                    catch (DbUpdateException ex)
                    {
                        return(Content(HttpStatusCode.InternalServerError, "There is an error when saving message to database"));
                    }

                    // Set up SignalR Hub to broadcast the FetchMessage command in receiver
                    var myHub = GlobalHost.ConnectionManager.GetHubContext <MessengerHub>();

                    // Get the message receiver's connection ID to broadcast FetchMessage command to
                    // Lookup by contactId
                    var connectionIDList = _messengerManager.GetConnectionIdWithUserId(contactUser.Id);


                    if (connectionIDList != null)
                    {
                        // When message is saved to database, it will be saved to both auth user's conversation and receiver's conversation
                        //Get the conversation id of the conversation that just receive the new message from auth user.
                        //This conversation is the one belongs to receiver/.
                        int conversationIdToFetchMessage = _messengerManager.GetConversationBetweenUsers(contactUser.Id, _authUserId).Id;

                        //Then broadcast that conversation id to the recever only using connection Id
                        //Then the receiver will know the which conversation that has new message ,and fetch the message
                        foreach (ChatConnectionMapping cM in connectionIDList)
                        {
                            var result = myHub.Clients.Client(cM.ConnectionId).FetchMessages(conversationIdToFetchMessage);
                        }
                        // string updatedToken = sm.RefreshSession(securityContext.Token);
                    }

                    var StoredMessageDTO = new StoredMessageDTO
                    {
                        ConversationId  = returnMessage.ConversationId,
                        OutgoingMessage = true,
                        SenderUsername  = authUsername,
                        MessageContent  = returnMessage.MessageContent,
                        CreatedDate     = returnMessage.CreatedDate,
                    };
                    return(Ok(new { message = StoredMessageDTO } /*new { SITtoken = updatedToken }*/));
                }
                else if (contactUser == null)
                {
                    return(Content(HttpStatusCode.NotFound, "Receiver does not exist to receive message"));
                }

                else if (contactUser.Id == _authUserId)
                {
                    return(Content(HttpStatusCode.Conflict, "You cannot send message to yourself"));
                }
                return(Content(HttpStatusCode.InternalServerError, "There is an error"));
            }
        }
        public IHttpActionResult GetRecentMessageWithUser(int conversationId2)
        {
            //The authentication part start from here
            //Create security context from the token
            UserManager     um = new UserManager();
            SecurityContext securityContext = SecurityContextBuilder.CreateSecurityContext(
                Request.Headers
                );

            if (securityContext == null)
            {
                return(Unauthorized());
            }

            //Validate the token
            SessionManager sm = new SessionManager();

            if (!sm.ValidateSession(securityContext.Token))
            {
                return(Unauthorized());
            }

            //Create authorization manager from security context to check claims
            AuthorizationManager authorizationManager = new AuthorizationManager(
                securityContext
                );

            List <string> requiredClaims = new List <string>()
            {
                "CanSendMessage"
            };

            if (!authorizationManager.CheckClaims(requiredClaims))
            {
                return(Unauthorized());
            }
            else
            {
                var authUsername = securityContext.UserName;

                //Get conversation between auth user and contact user
                var conversation = _messengerManager.GetConversationFromId(conversationId2);
                if (conversation != null)
                {
                    //Get contact user name
                    var contactUsername = conversation.ContactUsername;

                    //Return the most recent message from the conversation
                    var recentMessage  = _messengerManager.GetRecentMessageBetweenUser(conversationId2);
                    var senderUsername = "";


                    if (recentMessage != null)
                    {
                        //If true , the message is sent from auth user
                        //Set the display username in front end to auth username
                        if (recentMessage.OutgoingMessage == true)
                        {
                            senderUsername = authUsername;
                        }

                        //If false , set the display user name in front end to contactUsername
                        else
                        {
                            senderUsername = contactUsername;
                        }

                        //Create messageDTO for transger
                        var StoredMessageDTO = new StoredMessageDTO
                        {
                            Id             = recentMessage.Id,
                            ConversationId = conversationId2,
                            SenderUsername = senderUsername,
                            MessageContent = recentMessage.MessageContent,
                            CreatedDate    = recentMessage.CreatedDate
                        };

                        //return the message
                        return(Ok(new { message = StoredMessageDTO }));
                    }
                }


                return(Content(HttpStatusCode.NotFound, "No message in conversation"));
            }
        }