Пример #1
0
        public ActionResult Signup(UserViewModel model, string returnUrl)
        {
            // Lets first check if the Model is valid or not
            if (ModelState.IsValid)
            {
                using (UserDbContext entities = new UserDbContext())
                {
                    string username = model.username;
                    string password = Encryption.Encrypt(model.password);
                    bool userExists = entities.UserDbSet.Any(x => x.username == username);
                    if (userExists)
                    {
                        ModelState.AddModelError("", "The user with this login already exists");
                    }
                    else
                    {
                        UserEntityModel user = new UserEntityModel()
                        {
                            username = username,
                            password = password
                        };
                        UserRepository ur = new UserRepository(entities);
                        ur.InsertUser(user);
                        return RedirectToAction("Login", "Account");
                    }
                }
            }

            return View(model);
        }
Пример #2
0
        public ActionResult Login(UserViewModel model, string returnUrl)
        {
            using (UserDbContext entities = new UserDbContext())
            {
                string username = model.username;
                string password = Encryption.Encrypt(model.password);

                bool userValid =
                    entities.UserDbSet.Any(
                        user => user.username == username && user.password == password);

                if (userValid)
                {

                    FormsAuthentication.SetAuthCookie(username, true);
                    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);
        }