コード例 #1
0
        public void HomeController_BeginLoginWithUsernameAndPassword()
        {
            _memeberShipService.Setup(s => s.IsUserRegistered(It.Is <string>(p => p == "tester"))).Returns(true);
            _memeberShipService.Setup(s => s.GenerateServerChallenges(It.Is <string>(p => p == "tester")))
            .Returns(new List <ServerChallenge>
            {
                new ServerChallenge
                {
                    appId     = "unittests",
                    challenge = "notrealchallenge",
                    version   = "U2F_V2",
                    keyHandle = "notreallykeyhandle",
                }
            }).Verifiable();
            _memeberShipService.Setup(s => s.IsValidUserNameAndPassword(It.Is <string>(p => p == "tester"), It.Is <string>(p => p == "password"))).Returns(true).Verifiable();

            HomeController  homeController  = new HomeController(_memeberShipService.Object);
            BeginLoginModel beginLoginModel = new BeginLoginModel
            {
                UserName = "******",
                Password = "******"
            };

            ViewResult result = homeController.BeginLogin(beginLoginModel) as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(homeController.ModelState.IsValid);
            Assert.AreEqual("FinishLogin", result.ViewName);
            _memeberShipService.VerifyAll();
        }
コード例 #2
0
        public ActionResult BeginLogin(BeginLoginModel model)
        {
            if ((string.IsNullOrWhiteSpace(model.UserName) || string.IsNullOrWhiteSpace(model.Password)) ||
                (!_memeberShipService.IsUserRegistered(model.UserName.Trim()) &&
                 !_memeberShipService.IsValidUserNameAndPassword(model.UserName.Trim(), model.Password.Trim())))
            {
                // If we got this far, something failed, redisplay form
                ModelState.AddModelError("CustomError", "User has not been registered");
                return(View("Login", model));
            }

            try
            {
                ServerChallenge serverChallenge = _memeberShipService.GenerateServerChallenge(model.UserName.Trim());

                CompleteLoginModel loginModel = new CompleteLoginModel
                {
                    AppId     = serverChallenge.AppId,
                    KeyHandle = serverChallenge.KeyHandle,
                    Version   = serverChallenge.Version,
                    Challenge = serverChallenge.Challenge,
                    UserName  = model.UserName.Trim()
                };
                return(View("FinishLogin", loginModel));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            ModelState.AddModelError("CustomError", "User has not been registered");
            return(View("Login", model));
        }
コード例 #3
0
        public void HomeController_BeginLoginNoUsername()
        {
            HomeController  homeController  = new HomeController(_memeberShipService.Object);
            BeginLoginModel beginLoginModel = new BeginLoginModel();

            ViewResult result = homeController.BeginLogin(beginLoginModel) as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsFalse(homeController.ModelState.IsValid);
            Assert.IsFalse(homeController.ModelState.IsValid);
            Assert.AreEqual("Login", result.ViewName);
        }
コード例 #4
0
        public void HomeController_BeginLoginNoPassword()
        {
            HomeController  homeController  = new HomeController(_memeberShipService.Object);
            BeginLoginModel beginLoginModel = new BeginLoginModel
            {
                UserName = "******"
            };

            ViewResult result = homeController.BeginLogin(beginLoginModel) as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsFalse(homeController.ModelState.IsValid);
            Assert.AreEqual("Login", result.ViewName);
            Assert.AreEqual("tester", ((BeginLoginModel)result.Model).UserName);
        }
コード例 #5
0
        public void HomeController_BeginLoginExceptionThrown()
        {
            _memeberShipService.Setup(s => s.GenerateServerChallenge(It.IsAny <string>())).Throws(new Exception());
            _memeberShipService.Setup(s => s.IsUserRegistered(It.IsAny <string>())).Returns(true);

            HomeController  homeController  = new HomeController(_memeberShipService.Object);
            BeginLoginModel beginLoginModel = new BeginLoginModel {
                UserName = "******", Password = "******"
            };

            ViewResult result = homeController.BeginLogin(beginLoginModel) as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsFalse(homeController.ModelState.IsValid);
            Assert.AreEqual("Login", result.ViewName);
        }
コード例 #6
0
        public ActionResult BeginLogin(BeginLoginModel model)
        {
            if ((string.IsNullOrWhiteSpace(model.Password)) ||
                !_memeberShipService.IsUserRegistered(model.UserName.Trim()))
            {
                // If we got this far, something failed, redisplay form
                ModelState.AddModelError("CustomError", "User has not been registered.");
                return(View("Login", model));
            }

            if (!_memeberShipService.IsValidUserNameAndPassword(model.UserName.Trim(), model.Password.Trim()))
            {
                ModelState.AddModelError("CustomError", "User/Password is not invalid.");
                return(View("Login", model));
            }

            try
            {
                List <ServerChallenge> serverChallenge = _memeberShipService.GenerateServerChallenges(model.UserName.Trim());

                if (serverChallenge == null || serverChallenge.Count == 0)
                {
                    throw new Exception("No server challenges were generated.");
                }

                var challenges = JsonConvert.SerializeObject(serverChallenge);
                CompleteLoginModel loginModel = new CompleteLoginModel
                {
                    AppId      = serverChallenge.First().appId,
                    Version    = serverChallenge.First().version,
                    UserName   = model.UserName.Trim(),
                    Challenges = challenges
                };
                return(View("FinishLogin", loginModel));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

                ModelState.AddModelError("CustomError", e.Message);
                return(View("Login", model));
            }
        }
コード例 #7
0
        public void HomeController_BeginLoginException()
        {
            _memeberShipService.Setup(s => s.IsUserRegistered(It.Is <string>(p => p == "tester"))).Returns(true);
            _memeberShipService.Setup(s => s.IsValidUserNameAndPassword(It.Is <string>(p => p == "tester"), It.Is <string>(p => p == "password"))).Returns(true).Verifiable();
            _memeberShipService.Setup(s => s.GenerateServerChallenges(It.Is <string>(p => p == "tester")))
            .Returns(new List <ServerChallenge>());

            HomeController  homeController  = new HomeController(_memeberShipService.Object);
            BeginLoginModel beginLoginModel = new BeginLoginModel
            {
                UserName = "******",
                Password = "******"
            };

            ViewResult result = homeController.BeginLogin(beginLoginModel) as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsFalse(homeController.ModelState.IsValid);
            Assert.AreEqual("Login", result.ViewName);
        }