Example #1
0
        public ActionResult Create(Users users)
        {
            if (ModelState.IsValid)
            {
                service.Add(users);
                return RedirectToAction("Index");
            }

            return View(users);
        }
      //  [ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {

            if (ModelState.IsValid)
            {
                Users user = new Users()
                {
                    UserName = model.UserName,
                    Password = model.Password
                };
                if (Session["Captcha"] == null || Session["Captcha"].ToString() != model.Captcha)
                {
                    foreach (var modelValue in ModelState.Values)
                    {
                        modelValue.Errors.Clear();
                    }
                    ModelState.AddModelError("Captcha", "Wrong value of sum, please try again.");
                    return View(model);
                }

                if (service.CheckLogin(user))
                {
                    SetLoginSession(user);
                    Response.Redirect(FormsAuthentication.DefaultUrl, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("Error", "Username or Password didnot match");
                    return View();
                }
                //var users = new Users();
                //var user = service.CheckLogin(users);
                //if (user)
                //{
                //  //  await SignInAsync(user, model.RememberMe);
                //    return RedirectToLocal(returnUrl);
                //}
                //else
                //{
                //    ModelState.AddModelError("", "Invalid username or password.");
                //}
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        private void SetLoginSession(Users user)
        {

            var session = new UserSession(user.Id, user.UserName);
            UserSession.SetSession(session);
            int userid = UserSession.GetSession().UserId;
            FormsAuthentication.SetAuthCookie(user.UserName, false);
        }