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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. [AllowAnonymous]
  19. public ActionResult Index(string username)
  20. {
  21. ViewBag.Title = Config.Title + " - Profile";
  22. ViewBag.Message = "View Your Profile";
  23. return View();
  24. }
  25. [HttpGet]
  26. [AllowAnonymous]
  27. // GET: Profile
  28. public ActionResult Login()
  29. {
  30. return View(new LoginViewModel());
  31. }
  32. [HttpPost]
  33. [AllowAnonymous]
  34. public ActionResult Login(LoginViewModel model)
  35. {
  36. if (ModelState.IsValid)
  37. {
  38. string username = model.Username;
  39. string password = SHA384.Hash(model.Username, model.Password);
  40. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  41. if (userValid)
  42. {
  43. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  44. return Json(new { result = "true" });
  45. }
  46. }
  47. return Json(new { error = "Invalid User name or Password." });
  48. }
  49. public ActionResult Logout()
  50. {
  51. FormsAuthentication.SignOut();
  52. return RedirectToAction("Index", "Home", new { Area = "Home" });
  53. }
  54. [HttpPost]
  55. [AllowAnonymous]
  56. public ActionResult Register(RegisterViewModel model)
  57. {
  58. if (ModelState.IsValid)
  59. {
  60. var foundUser = db.Users.Where(b => b.Username == model.Username);
  61. if (foundUser.Any())
  62. {
  63. return Json(new { error = "That username already exists." });
  64. }
  65. if (model.Password != model.ConfirmPassword)
  66. {
  67. return Json(new { error = "Passwords must match." });
  68. }
  69. if (model.Insert())
  70. {
  71. return RedirectToAction("Login", "Profile", new LoginViewModel { Username = model.Username, Password = model.Password });
  72. }
  73. return Json(new { error = "You must include all fields." });
  74. }
  75. return Json(new { error = "You must include all fields." });
  76. }
  77. }
  78. }