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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(string ReturnUrl)
  29. {
  30. LoginViewModel model = new LoginViewModel();
  31. model.ReturnUrl = ReturnUrl;
  32. return View("/Areas/Profile/Views/Profile/ViewLogin.cshtml", model);
  33. }
  34. [HttpPost]
  35. [AllowAnonymous]
  36. public ActionResult Login(LoginViewModel model)
  37. {
  38. if (ModelState.IsValid)
  39. {
  40. string username = model.Username;
  41. string password = SHA384.Hash(model.Username, model.Password);
  42. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  43. if (userValid)
  44. {
  45. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  46. if (string.IsNullOrEmpty(model.ReturnUrl))
  47. {
  48. return Json(new { result = "true" });
  49. }
  50. else
  51. {
  52. return Redirect(model.ReturnUrl);
  53. }
  54. }
  55. }
  56. return Json(new { error = "Invalid User name or Password." });
  57. }
  58. public ActionResult Logout()
  59. {
  60. FormsAuthentication.SignOut();
  61. return RedirectToAction("Index", "Home", new { Area = "Home" });
  62. }
  63. [HttpPost]
  64. [AllowAnonymous]
  65. public ActionResult Register(RegisterViewModel model)
  66. {
  67. if (ModelState.IsValid)
  68. {
  69. var foundUser = db.Users.Where(b => b.Username == model.Username);
  70. if (foundUser.Any())
  71. {
  72. return Json(new { error = "That username already exists." });
  73. }
  74. if (model.Password != model.ConfirmPassword)
  75. {
  76. return Json(new { error = "Passwords must match." });
  77. }
  78. if (model.Insert())
  79. {
  80. return RedirectToAction("Login", "Profile", new LoginViewModel { Username = model.Username, Password = model.Password });
  81. }
  82. return Json(new { error = "You must include all fields." });
  83. }
  84. return Json(new { error = "You must include all fields." });
  85. }
  86. }
  87. }