123 lines
3.5 KiB
C#
123 lines
3.5 KiB
C#
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Net.Mail;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Web.Mvc;
|
|
using Portoa.Web.Controllers;
|
|
using Portoa.Web.Security;
|
|
using VideoGameQuotes.Api;
|
|
using VideoGameQuotes.Web.Models;
|
|
|
|
namespace VideoGameQuotes.Web.Controllers {
|
|
public class HomeController : Controller {
|
|
private readonly IAuthenticationService authenticationService;
|
|
private readonly ICurrentUserProvider userProvider;
|
|
|
|
private static readonly string[] answers = new[] {
|
|
"I AM ERROR.",
|
|
"shyron",
|
|
"our princess is in another castle",
|
|
"the cake is a lie",
|
|
"all your base",
|
|
"ganon not gannon",
|
|
"thunderbird",
|
|
"'glad you came, pit!",
|
|
"ryu huyabasa"
|
|
};
|
|
|
|
public HomeController(IAuthenticationService authenticationService, ICurrentUserProvider userProvider) {
|
|
this.authenticationService = authenticationService;
|
|
this.userProvider = userProvider;
|
|
}
|
|
|
|
public ActionResult Index() {
|
|
return View();
|
|
}
|
|
|
|
public ActionResult About() {
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult Login([Required]string username, [Required]string password) {
|
|
if (!ModelState.IsValid) {
|
|
return Json(this.CreateJsonErrorResponse("Invalid request"));
|
|
}
|
|
if (!authenticationService.IsValid(username, password)) {
|
|
return Json(this.CreateJsonErrorResponse("Invalid username/password"));
|
|
}
|
|
|
|
authenticationService.Login(username);
|
|
return Json(this.CreateJsonResponse());
|
|
}
|
|
|
|
[ChildActionOnly]
|
|
public ActionResult MainMenu() {
|
|
var model = new MainMenuModel { User = userProvider.CurrentUser };
|
|
return PartialView("MainMenu", model);
|
|
}
|
|
|
|
public ActionResult Logout(string redirectUrl) {
|
|
authenticationService.Logout();
|
|
return Redirect(redirectUrl ?? "/");
|
|
}
|
|
|
|
public ActionResult Contact() {
|
|
var randomAnswer = GetRandomAnswer();
|
|
var model = new ContactModel {
|
|
UnhashedCaptchaAnswer = randomAnswer,
|
|
HashedCaptchaAnswer = GetHashedCaptcha(randomAnswer)
|
|
};
|
|
|
|
return View(model);
|
|
}
|
|
|
|
private static string GetRandomAnswer() {
|
|
return answers[new Random().Next(answers.Length)];
|
|
}
|
|
|
|
private static string GetHashedCaptcha(string value) {
|
|
return Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(value ?? string.Empty)));
|
|
}
|
|
|
|
private static void ResetModel(ContactModel model) {
|
|
model.UnhashedCaptchaAnswer = GetRandomAnswer();
|
|
model.HashedCaptchaAnswer = GetHashedCaptcha(model.UnhashedCaptchaAnswer);
|
|
model.CaptchaAnswer = null;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult Contact(ContactModel model) {
|
|
if (GetHashedCaptcha(model.CaptchaAnswer) != model.HashedCaptchaAnswer) {
|
|
ModelState.AddModelError("CaptchaAnswer", "You are not human");
|
|
}
|
|
|
|
if (!ModelState.IsValid) {
|
|
ResetModel(model);
|
|
return View(model);
|
|
}
|
|
|
|
//send email
|
|
var fromAddress = new MailAddress("contact@tommymontgomery.com", "Contact Bot");
|
|
var subject = string.Format("[tommymontgomery.com] Message from {0}", model.Name);
|
|
|
|
var client = new SmtpClient {
|
|
Host = "localhost"
|
|
};
|
|
|
|
var message = new MailMessage(fromAddress, new MailAddress("tmont@tmont.com")) { Subject = subject, Body = model.Message };
|
|
message.ReplyToList.Add(new MailAddress(model.Email, model.Name));
|
|
try {
|
|
client.Send(message);
|
|
} catch (Exception e) {
|
|
ModelState.AddModelError("client", e.Message);
|
|
ResetModel(model);
|
|
return View(model);
|
|
}
|
|
|
|
return View("ContactSuccess");
|
|
}
|
|
}
|
|
}
|