Ejemplo n.º 1
0
        public void AccountControllerLogin()
        {
            const bool ISAJAX = true;
            var info = new LoginModel()
            {
                Email = "Admin",
                Password = "******"
            };
            string returnUrl = "returnUrl";

            var factory = new WorkChannelFactoryMock();
            var auth = new FakeAuthenticationService();
            var ctrl = new AccountController(factory, auth);
            FakeControllerSession.SetFakeControllerContext(ctrl, ISAJAX);

            //Pass if everything ok
            var res2 = ctrl.Login(info, returnUrl) as RedirectToRouteResult;
            Assert.IsNotNull(res2);
            Assert.AreEqual(res2.RouteValues["action"], "Index");

            var user = ctrl.Session["User"] as UserDTO;
            Assert.IsNotNull(user);
            Assert.AreEqual(user.Email, "Admin");
            Assert.AreEqual(user.Password, "123");

            //Fault when wrong user
            info.Email = "unknown";
            var res3 = ctrl.Login(info, returnUrl) as ViewResult;
            Assert.IsNotNull(res3);
            Assert.AreEqual(res3.Model, "wrong user");
        }
Ejemplo n.º 2
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid
                //  && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)
                )
            {
                try
                {
                    using (var proxy = factory.GetWorkChannel())
                    {
                        var user = proxy.GetUser(new PizzaNetCommon.Requests.EmptyRequest
                        {
                            Login = model.Email,
                            Password = model.Password
                        });
                        this.Session["User"] = user.Data;
                        //this.Session["Email"] = user.Data.Email;
                        //this.Session["Password"] = user.Data.Password;
                        this.Session["LoggedIn"] = true;
                        this.ViewBag.Name = user.Data.Name;
                        this.ViewBag.LoggedIn = true;
                        auth.SetAuthCookie(user.Data.Email, false);
                    }
                }
                catch (Exception e)
                {
                    return HandleFaults(e);
                }
                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);
        }