The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProfileController.cs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Security;
  7. using Teknik.Areas.Profile.ViewModels;
  8. using Teknik.Controllers;
  9. using Teknik.Helpers;
  10. using Teknik.Models;
  11. using Teknik.ViewModels;
  12. namespace Teknik.Areas.Profile.Controllers
  13. {
  14. public class ProfileController : DefaultController
  15. {
  16. private TeknikEntities db = new TeknikEntities();
  17. // GET: Profile/Profile
  18. public ActionResult Index(string username)
  19. {
  20. ViewBag.Title = Config.Title + " - Profile";
  21. ViewBag.Message = "View Your Profile";
  22. return View();
  23. }
  24. [HttpGet]
  25. [AllowAnonymous]
  26. // GET: Profile
  27. public ActionResult Login()
  28. {
  29. return View();
  30. }
  31. [HttpPost]
  32. [AllowAnonymous]
  33. public ActionResult Login(LoginViewModel model)
  34. {
  35. if (ModelState.IsValid)
  36. {
  37. string username = model.Username;
  38. string password = SHA384.Hash(model.Username, model.Password);
  39. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  40. if (userValid)
  41. {
  42. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  43. return Json(new { result = "true" });
  44. }
  45. }
  46. return Json(new { error = "Invalid User name or Password." });
  47. }
  48. public ActionResult Logout()
  49. {
  50. FormsAuthentication.SignOut();
  51. return RedirectToAction("Index", "Home", new { Area = "Home" });
  52. }
  53. [HttpPost]
  54. [AllowAnonymous]
  55. public ActionResult Register(RegisterViewModel model)
  56. {
  57. if (ModelState.IsValid)
  58. {
  59. var foundUser = db.Users.Where(b => b.Username == model.Username);
  60. if (foundUser.Any())
  61. {
  62. return Json(new { error = "That username already exists." });
  63. }
  64. if (model.Password != model.ConfirmPassword)
  65. {
  66. return Json(new { error = "Passwords must match." });
  67. }
  68. if (model.Insert())
  69. {
  70. return RedirectToAction("Login", "Profile", new LoginViewModel { Username = model.Username, Password = model.Password });
  71. }
  72. return Json(new { error = "You must include all fields." });
  73. }
  74. return Json(new { error = "You must include all fields." });
  75. }
  76. }
  77. }