public void CheckLoginHistoryListNonFiltered()
        {
            //Arrange
            InitializeAutoMapper();
            var mock = new Mock <ILoginAttemptRepository>();

            mock.SetupGet(m => m.LoginAttempts)
            .Returns(new[]
            {
                new LoginAttempt {
                    Email = "*****@*****.**", Name = "iain", LoginSuccess = false, LoginAttemptTime = DateTime.Now, Id = 1
                },
                new LoginAttempt {
                    Email = "*****@*****.**", Name = "iain", LoginSuccess = false, LoginAttemptTime = DateTime.Now, Id = 2
                },
                new LoginAttempt {
                    Email = "*****@*****.**", Name = "saul", LoginSuccess = true, LoginAttemptTime = DateTime.Now, Id = 3
                }
            }.AsQueryable());

            var controller = new UserLoginController(mock.Object);

            //Act
            var result = controller.UserLoginList(null, null);

            //Assert
            Assert.True(result.GetType().Name == nameof(ViewResult));
            var viewResult = (ViewResult)result;
            var data       = (IEnumerable <LoginAttemptViewModel>)viewResult.Model;

            Assert.True(data.Count() == 3);
        }
        public void Lockout2WithinLastHourWorks()
        {
            //Arrange
            InitializeAutoMapper();
            var mock = new Mock <ILoginAttemptRepository>();

            mock.SetupGet(m => m.LoginAttempts)
            .Returns(new[]
            {
                new LoginAttempt {
                    Email = "*****@*****.**", Name = "iain", LoginSuccess = false, LoginAttemptTime = DateTime.Now, Id = 1
                },
                new LoginAttempt {
                    Email = "*****@*****.**", Name = "iain", LoginSuccess = false, LoginAttemptTime = DateTime.Now, Id = 2
                }
            }.AsQueryable());

            var controller = new UserLoginController(mock.Object);

            //Act
            var result = controller.UserLogin(new UserLoginViewModel {
                Dob = DateTime.Now.AddYears(-20), Email = "*****@*****.**", Name = "iain"
            });

            //Assert
            Assert.True(result.GetType().Name == nameof(RedirectToActionResult));
            var actionResult = (RedirectToActionResult)result;

            Assert.True(actionResult.ActionName == "UserLoginList");
        }
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
            else
            {
                string   authenticationToken        = actionContext.Request.Headers.Authorization.Parameter;
                string   decodedAuthenticationToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken));
                string[] usernamePasswordArray      = decodedAuthenticationToken.Split(':');
                string   username = usernamePasswordArray[0];
                string   password = usernamePasswordArray[1];

                if (UserLoginController.Login(username, password))
                {
                    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), null);
                }
                else
                {
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            UserLoginController controller = new UserLoginController();

            controller.Run();
        }