示例#1
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());
            }
        }
示例#2
0
 // Send method which identifies the sender and stores messages within MongoDB and broadcasts messages to all active clients
 public void Send(string sessionId, string message)
 {
     if (message != "")
     {
         MongoCRUD    database     = new MongoCRUD("UserBase");
         var          thisUser     = database.LoadRecordById <SessionModel>("Sessions", sessionId);
         var          username     = thisUser.usermodel.Username;
         MessageModel messagemodel = new MessageModel {
             Message = message, usermodel = thisUser.usermodel, MessageTime = new BsonDateTime(DateTime.Now)
         };
         database.InsertRecord("Messages", messagemodel);
         // Call the broadcastMessage method to update clients
         Clients.All.broadcastMessage(username, message);
     }
 }