예제 #1
0
        public bool Logout(Stream userData)
        {
            // First validate the user priviledges
            var         reader        = new StreamReader(userData);
            string      content       = reader.ReadToEnd();
            LCPostModel postDataModel = JsonConvert.DeserializeObject <LCPostModel>(content);

            if (validateUser(postDataModel) == false)
            {
                return(false);
            }
            IEnumerable <Session> sessions;

            using (var authDbEntities = new LCAuthDBEntities())
            {
                // Find the user from the AuthDB
                string userEmail = postDataModel.Email;
                sessions = authDbEntities.Sessions.Where(s => s.Email == userEmail);
                foreach (var s in sessions)
                {
                    authDbEntities.Sessions.Remove(s);
                }
                authDbEntities.SaveChanges();
                return(true);
            }
        }
예제 #2
0
        public string Login(Stream loginData)
        {
            // convert Stream Data to StreamReader
            var         reader        = new StreamReader(loginData);
            string      content       = reader.ReadToEnd();
            LCPostModel postDataModel = JsonConvert.DeserializeObject <LCPostModel>(content);
            UserModel   userModel     = JsonConvert.DeserializeObject <UserModel>(postDataModel.ContentData);

            using (var authDbEntities = new LCAuthDBEntities())
            {
                // Find the user from the AuthDB
                string name = userModel.email;
                User   user = authDbEntities.Users.FirstOrDefault(u => u.Email == name);
                if (user == null)
                {
                    return(null);
                }
                if (user.Password != userModel.password)
                {
                    return(null);
                }
                else
                {
                    Session session = authDbEntities.Sessions.FirstOrDefault(u => u.Email == name);
                    if (session != null)
                    {
                        return(null);
                    }
                    string token = generateToken();
                    string ip    = getIP();
                    // Add the user to the user pool
                    Session entry = new Session()
                    {
                        Email       = name,
                        SessionID   = 0,
                        StartTime   = DateTime.Now,
                        IP          = ip,
                        AccessToken = token,
                        Duration    = 60
                    };
                    authDbEntities.Sessions.Add(entry);
                    authDbEntities.SaveChanges();
                    return(token);
                }
            }
        }
예제 #3
0
        public string Join(Stream data)
        {
            // convert Stream Data to StreamReader
            var         reader    = new StreamReader(data);
            string      content   = reader.ReadToEnd();
            LCPostModel postModel = JsonConvert.DeserializeObject <LCPostModel>(content);
            UserModel   userModel = JsonConvert.DeserializeObject <UserModel>(postModel.ContentData);

            // Check if the user email already exists
            User user = null;

            using (var authDbEntities = new LCAuthDBEntities())
            {
                user = authDbEntities.Users.FirstOrDefault(u => u.Email == userModel.email);

                if (user != null)
                {
                    return("The email address has been used.");
                }

                // Create new user
                User newUser = new User()
                {
                    Email    = userModel.email,
                    Password = userModel.password
                };

                authDbEntities.Users.Add(newUser);
                authDbEntities.SaveChanges();
            }
            using (dbEntities)
            {
                LCUser newLCUser = new LCUser()
                {
                    Email    = userModel.email,
                    UserName = userModel.username,
                    Enabled  = true
                };
                dbEntities.LCUsers.Add(newLCUser);
                dbEntities.SaveChanges();
            }
            return("Registration Successful!");
        }