Пример #1
0
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            int s = 0;
            if (ModelState.IsValid)
            {
                List<User> qr2List = db.Users.ToList<User>();
                User user = new User();
                user = qr2List.Find(delegate(User qr2Del) {
                    if (qr2Del.Username == model.UserName)
                    {
                        s = 1;
                        return qr2Del.Username == model.UserName;

                    }
                    return qr2Del.Username == model.UserName;

                });
                // et nullreferenceexeptionist kõrvale hiilida.
                if (s == 0)
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    return View(model);
                }
                    if (user.Password == model.Password)
                    {

                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    }
                }

            return View(model);
        }
Пример #2
0
        public ActionResult Register(User user)
        {
            int s = 0;
            if (ModelState.IsValid)
            {
                List<User> qr2List = db.Users.ToList<User>();
                User user2 = new User();
                user2 = qr2List.Find(delegate(User qr2Del)
                {
                    if (qr2Del.Username == user.Username)
                    {
                        s = 1;
                        return qr2Del.Username == user.Username;

                    }
                    else
                    {
                        s = 2;
                        return qr2Del.Username != user.Username;
                    }
                });

                if (s == 2)
                {

                    db.Users.Add(user);
                    db.SaveChanges();
                    return RedirectToAction("LogOn");
                }
                else
                {

                   ModelState.AddModelError("", "Username already exists!");

                }

            }

            return View();
        }