Beispiel #1
0
        // Load method which references MongoDB database and loads lists of messages and active session users
        public void Load()
        {
            MongoCRUD database    = new MongoCRUD("UserBase");
            var       messageList = database.LoadRecords <MessageModel>("Messages");

            foreach (var MessageModel in messageList)
            {
                Clients.Caller.broadcastMessage(MessageModel.usermodel.Username, MessageModel.Message);
            }
            var sessionlist = database.LoadRecords <SessionModel>("Sessions");

            foreach (var SessionModel in sessionlist)
            {
                Clients.Caller.broadcastActiveUser(SessionModel.usermodel.Username);
            }
        }
Beispiel #2
0
        // Logout method which removes relevant sessions
        public void Logout(string sessionId)
        {
            MongoCRUD database    = new MongoCRUD("UserBase");
            var       sessionList = database.LoadRecords <SessionModel>("Sessions");

            foreach (var SessionModel in sessionList)
            {
                if (sessionId == SessionModel.Id.ToString())
                {
                    database.DeleteRecordById <SessionModel>("Sessions", SessionModel.Id.ToString());
                    Clients.All.RefreshPage();
                }
            }
        }
Beispiel #3
0
        // Login method with authentication and storage of user within MongoDB
        public string Login(string username, string password)
        {
            UserModel usermodel = new UserModel {
                Username = username, Password = password
            };

            if (usermodel.Username == "" || usermodel.Password == "")
            {
                Console.WriteLine("empty fields");
                return("false");
            }
            else
            {
                // Creating session upon login success
                MongoCRUD    database     = new MongoCRUD("UserBase");
                SessionModel sessionmodel = new SessionModel {
                    Id = Context.ConnectionId, usermodel = usermodel
                };
                var userList = database.LoadRecords <UserModel>("Users");
                foreach (var UserModel in userList)
                {
                    if (usermodel.Username == UserModel.Username)
                    {
                        if (usermodel.Password == UserModel.Password)
                        {
                            database.InsertRecord("Sessions", sessionmodel);
                            Clients.All.RefreshPage();
                            return(sessionmodel.Id.ToString());
                        }
                        else
                        {
                            return("false");
                        }
                    }
                }
                database.InsertRecord("Users", usermodel);
                database.InsertRecord("Sessions", sessionmodel);
                Clients.All.RefreshPage();
                return(sessionmodel.Id.ToString());
            }
        }