public LoginModule(PeopleContext people, Ldap ldap)
        {
            _people = people;
            _ldap = ldap;

            Get["/login"] = parameters =>
            {
                // Called when the user visits the login page or is redirected here because
                // an attempt was made to access a restricted resource. It should return
                // the view that contains the login form
                return View["login"];
            };

            Get["/logoff"] = parameters =>
            {
                // Called when the user clicks the sign out button in the application. Should
                // perform one of the Logout actions (see below)
                return this.LogoutAndRedirect("/");
            };

            Post["/login"] = parameters =>
            {
                // Called when the user submits the contents of the login form. Should
                // validate the user based on the posted form data, and perform one of the
                // Login actions (see below)
                var username = (string) Request.Form.username;
                var password = (string) Request.Form.password;

                // Authenticate user against AD
                if (!_ldap.IsAuthenticated(ConfigurationManager.AppSettings.Get("ldap-domain"), username, password))
                {
                    return View["login", "Unable to validate your account. Please contact the dev team at [email protected]"];
                }

                var user = _people.People.FirstOrDefault(p => p.AdUser == username && !p.Hidden && !p.Retired);
                if (user == null)
                {
                    // User was not found in the database, register the ad user.
                    var newUser = ldap.GetUser(username);
                    user = new Person
                    {
                        Id = Guid.NewGuid(),
                        AdUser = username,
                        Created = DateTime.Now,
                        Email = newUser.Properties["mail"][0].ToString(),
                        Name = newUser.Properties["displayName"][0].ToString()
                    };
                    user = _people.People.Add(user);
                    _people.SaveChanges();
                }

                return this.LoginAndRedirect(user.Id, null, "/profile");
            };
        }
        public LoginModule(PeopleContext people, Ldap ldap)
        {
            _people = people;
            _ldap   = ldap;

            Get["/login"] = parameters =>
            {
                // Called when the user visits the login page or is redirected here because
                // an attempt was made to access a restricted resource. It should return
                // the view that contains the login form
                return(View["login"]);
            };

            Get["/logoff"] = parameters =>
            {
                // Called when the user clicks the sign out button in the application. Should
                // perform one of the Logout actions (see below)
                return(this.LogoutAndRedirect("/"));
            };

            Post["/login"] = parameters =>
            {
                // Called when the user submits the contents of the login form. Should
                // validate the user based on the posted form data, and perform one of the
                // Login actions (see below)
                var username = (string)Request.Form.username;
                var password = (string)Request.Form.password;

                // Authenticate user against AD
                if (!_ldap.IsAuthenticated(ConfigurationManager.AppSettings.Get("ldap-domain"), username, password))
                {
                    return(View["login", "Unable to validate your account. Please contact the dev team at [email protected]"]);
                }

                var user = _people.People.FirstOrDefault(p => p.AdUser == username && !p.Hidden && !p.Retired);
                if (user == null)
                {
                    // User was not found in the database, register the ad user.
                    var newUser = ldap.GetUser(username);
                    user = new Person
                    {
                        Id      = Guid.NewGuid(),
                        AdUser  = username,
                        Created = DateTime.Now,
                        Email   = newUser.Properties["mail"][0].ToString(),
                        Name    = newUser.Properties["displayName"][0].ToString()
                    };
                    user = _people.People.Add(user);
                    _people.SaveChanges();
                }

                return(this.LoginAndRedirect(user.Id, null, "/profile"));
            };
        }