Пример #1
0
        public JsonResult Login(string UserName, string Password)
        {
            string message = "";
            try
            {
                using (Models.MembershipEntities1 memberDatabase = new MembershipEntities1())
                {
                    
                    if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
                    {

                        Models.User user = memberDatabase.Users.Where(User => User.UserName == UserName && User.Password == Password).SingleOrDefault();
                        if(user == null)
                        {
                            message = "The username or password you entered was invalid";
                        }
                        else
                        {
                            Session["userId"] = user.ID.ToString();
                        }
                    }
                    else
                    {
                        throw new Exception("Username and password must have a value");
                    }

                }
            }
            catch (Exception ex) { message = ex.Message; }


            return Json(message, JsonRequestBehavior.AllowGet);
       
        }
Пример #2
0
        public void MembershipEntitiesTest()
        {
            using (MembershipEntities1 memberDatabase = new MembershipEntities1())
            {

               User newuser = memberDatabase.Users.FirstOrDefault();

               Assert.AreNotEqual(null, newuser);
            }
        }
Пример #3
0
        public JsonResult AddUserFavorite(string articleID)
        {
            int userId = Convert.ToInt32(Session["userId"]);
            string message = "";

            using (Models.MembershipEntities1 memberDatabase = new MembershipEntities1())
            {
                memberDatabase.UserArticles.Add(new UserArticle { ArticleId = Convert.ToInt32(articleID), UserId = userId });
                try
                {
                    memberDatabase.SaveChanges();
                }
                catch (Exception)
                {
                    message = "Article has already been added to favorites";
                }
               
            }
            return Json(message, JsonRequestBehavior.AllowGet);
        }
Пример #4
0
        public JsonResult RegisterUser(string UserName, string Password, string EmailAddress)
        {
            string message = "";                                                                                                
            try
            {
                using (Models.MembershipEntities1 memberDatabase = new MembershipEntities1())
                {
                    if(!EmailAddress.Contains('@') || (!EmailAddress.Contains(".com")))
                    {
                        throw new Exception("Must have valid Email");
                    }
                    else if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(EmailAddress))
                    {
                        Models.User newuser = new Models.User
                        {
                            Email = EmailAddress,
                            UserName = UserName,
                            Password = Password
                        };
                        memberDatabase.Users.Add(newuser);
                        try
                        {
                            memberDatabase.SaveChanges();
                        }
                        catch(Exception ex)
                        {
                            message = "User already exists";
                        }
                       
                    }
                    else
                    {
                        throw new Exception("Username, password, and email must have a value");
                    }

                }
            }
            catch (Exception ex) { message = ex.Message; }

               
                return Json(message, JsonRequestBehavior.AllowGet);
                // If we got this far, something failed, redisplay form

            }
Пример #5
0
        public ActionResult UserFavorites()
        {
            List<int> favorites = null;

            int ID = Convert.ToInt32(Session["userId"]);
            using (Models.MembershipEntities1 memberDatabase = new MembershipEntities1())
            {
                favorites = memberDatabase.UserArticles.Where(User => User.UserId == ID).Select(Article => Article.ArticleId).ToList();

            }
            var articles = db.T_Article.Where(Article => favorites.Contains(Article.ID)).ToList();
            //var articles = from article in db.T_Article
            //               select article;

            return View(articles);
        }