Esempio n. 1
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            //if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            //{
            //    return RedirectToLocal(returnUrl);
            //}

            //// If we got this far, something failed, redisplay form
            //ModelState.AddModelError("", "The user name or password provided is incorrect.");
            //return View(model);
            if (ModelState.IsValid)
            {
                UserBusinessLayer userBusinessLayer = new UserBusinessLayer();
                User loggedinUser = userBusinessLayer.GetUserDetails(model.UserName, model.Password);
                if (loggedinUser.UserId == 0)
                {
                    ModelState.AddModelError("InvalidLogin", "Invalid Username or Password !!");
                    return(View(model));
                }
                else
                {
                    Session["UserId"]          = loggedinUser.UserId;
                    Session["Username"]        = model.UserName;
                    Session["UserDisplayName"] = loggedinUser.UserDisplayName;
                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View(model));
        }
Esempio n. 2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    //WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    //WebSecurity.Login(model.UserName, model.Password);
                    //return RedirectToAction("Index", "Home");
                    UserBusinessLayer userBusinessLayer = new UserBusinessLayer();
                    User registerUser = userBusinessLayer.GetUserDetails(model.UserName, model.Password);
                    if (registerUser.UserId == 0)
                    {
                        userBusinessLayer.RegisterUser(model.UserName, model.Password, model.DisplayName);
                        return(RedirectToAction("Login", "Account"));
                    }
                    else
                    {
                        ModelState.AddModelError("UserExists", "This username already exists !!");
                        return(View(model));
                    }
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }