public ActionResult ForgottenPassword(AuthForgottenPasswordViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return View("ForgottenPassword", viewModel);
            }

            using (var context = new RowanHouseDb())
            {
                var account = context.Users.FirstOrDefault(e => e.Username.ToLower().Equals(viewModel.Username.ToLower()));
                account.Password = PasswordGenerator.Generate();
                context.Entry(account).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
            }

            return RedirectToAction("LogIn");
        }
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            Username = Username.Trim();

            if (string.IsNullOrEmpty(Username))
            {
                yield return new ValidationResult("You haven't entered your username so we can't identify your account.");
            }

            using (var context = new RowanHouseDb())
            {
                if (!context.Users.Any(e => e.Username.ToLower().Equals(Username.ToLower())))
                {
                    yield return new ValidationResult("There is no account with the username specified.");
                }
            }
        }
 public ActionResult Index(string entryId)
 {
     using (RowanHouseDb db = new RowanHouseDb())
     {
         var viewModel = new BlogEntriesIndexViewModel() {
             BlogEntryList = db.BlogEntries.OrderByDescending(e => e.DatePublished).ToList(),
             SelectedID = entryId
         };
         if (!string.IsNullOrEmpty(entryId))
         {
             var page = db.BlogEntries.FirstOrDefault(e => e.ID.ToString() == entryId);
             viewModel.Title = page.Title;
             viewModel.Url = page.Url;
             viewModel.Content = page.Content;
             viewModel.ParsedContent =_markdown.Transform(viewModel.Content);
         }
         return View("Index", viewModel);
     }
 }
 public ActionResult Index(string url)
 {
     using (RowanHouseDb db = new RowanHouseDb())
     {
         BlogEntry requestedEntry = null;
         if (string.IsNullOrEmpty(url))
         {
             requestedEntry = db.BlogEntries.First(e => e.DatePublished == db.BlogEntries.Min(x => x.DatePublished));
         }
         else
         {
             requestedEntry = db.BlogEntries.First(e => e.Url.ToLower().Equals(url.ToLower()));
         }
         var viewModel = new HomeIndexViewModel() {
             RequestedEntry = requestedEntry,
             BlogEntries = db.BlogEntries.Include("Author").OrderByDescending(e => e.DatePublished).ToList()
         };
         return View("Index", viewModel);
     }
 }
        public ActionResult Index(BlogEntriesIndexViewModel viewModel)
        {
            using (RowanHouseDb db = new RowanHouseDb())
            {
                viewModel.BlogEntryList = db.BlogEntries.OrderByDescending(e => e.DatePublished).ToList();
                if (!ModelState.IsValid)
                {
                    return View("Index", viewModel);
                }

                BlogEntry entry = null;
                if (!string.IsNullOrEmpty(viewModel.SelectedID))
                {
                    entry = db.BlogEntries.First(e => e.ID.ToString() == viewModel.SelectedID);
                    entry.Title = viewModel.Title;
                    entry.Url = viewModel.Url;
                    entry.Content = viewModel.Content;
                    entry.Author = db.Users.FirstOrDefault(e => e.Username.ToLower().Equals(User.Identity.Name.ToLower()));
                    entry.DatePublished = DateTime.Now;
                    db.Entry(entry).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    entry = new BlogEntry() {
                        Title = viewModel.Title,
                        Url = viewModel.Url,
                        Content = viewModel.Content,
                        Author = db.Users.FirstOrDefault(e => e.Username.ToLower().Equals(User.Identity.Name.ToLower())),
                        DatePublished = DateTime.Now
                    };
                    db.BlogEntries.Add(entry);
                }
                db.SaveChanges();
                return RedirectToRoute("Blog_Entries", new { url = entry.Url });
            }
        }
        public ActionResult LogIn(AuthLogInViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return View("LogIn", viewModel);
            }

            string hashedPassword = PasswordGenerator.Hash(viewModel.Password);
            using (RowanHouseDb db = new RowanHouseDb())
            {
                var user = db.Users.FirstOrDefault(e => e.Username.ToLower().Equals(viewModel.Username.ToLower()) && e.Password.Equals(hashedPassword));
                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(user.Username, false);
                    if (Request.QueryString["returnUrl"] != null)
                    {
                        return Redirect(Request.QueryString["returnUrl"]);
                    }
                    return Redirect(FormsAuthentication.DefaultUrl);
                }
                ModelState.AddModelError(string.Empty, "There was no user that matches the username and password specified.");
                return View("LogIn", viewModel);
            }
        }
        public ActionResult Index(string url)
        {
            using (RowanHouseDb db = new RowanHouseDb())
            {
                WebPage requestedPage = null;
                if (string.IsNullOrEmpty(url))
                {
                    requestedPage = db.WebPages.First(e => e.Ordinal == 1);
                }
                else
                {
                    requestedPage = db.WebPages.First(e => e.Url.ToLower() == url.ToLower());
                }
                var navigableWebPages = db.WebPages.Where(e => e.IsTopLevelPage).OrderBy(e => e.Ordinal).ToList();
                navigableWebPages.Add(new WebPage() { Title = "Our Blog", Url = "blog" });
                var viewModel = new WebPageViewModel() {
                    CarouselImages = Directory.GetFiles(Server.MapPath("~/App_Data/Images"), "carousel-*").Select(e => Path.GetFileNameWithoutExtension(e)),
                    WebPages = navigableWebPages,
                    SelectedWebPage = requestedPage
                };

                return View("Index", viewModel);
            }
        }