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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Blog.Models;
  8. using Teknik.Areas.Profile.Models;
  9. using Teknik.Areas.Profile.ViewModels;
  10. using Teknik.Controllers;
  11. using Teknik.Helpers;
  12. using Teknik.Models;
  13. using Teknik.ViewModels;
  14. namespace Teknik.Areas.Profile.Controllers
  15. {
  16. public class ProfileController : DefaultController
  17. {
  18. private TeknikEntities db = new TeknikEntities();
  19. // GET: Profile/Profile
  20. [AllowAnonymous]
  21. public ActionResult Index(string username)
  22. {
  23. ViewBag.Title = Config.Title + " - Profile";
  24. ViewBag.Message = "View Your Profile";
  25. return View(new ProfileViewModel());
  26. }
  27. [HttpGet]
  28. [AllowAnonymous]
  29. // GET: Profile
  30. public ActionResult Login(string ReturnUrl)
  31. {
  32. LoginViewModel model = new LoginViewModel();
  33. model.ReturnUrl = ReturnUrl;
  34. return View("/Areas/Profile/Views/Profile/ViewLogin.cshtml", model);
  35. }
  36. [HttpPost]
  37. [AllowAnonymous]
  38. public ActionResult Login(LoginViewModel model)
  39. {
  40. if (ModelState.IsValid)
  41. {
  42. string username = model.Username;
  43. string password = SHA384.Hash(model.Username, model.Password);
  44. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  45. if (userValid)
  46. {
  47. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  48. if (string.IsNullOrEmpty(model.ReturnUrl))
  49. {
  50. return Json(new { result = "true" });
  51. }
  52. else
  53. {
  54. return Redirect(model.ReturnUrl);
  55. }
  56. }
  57. }
  58. return Json(new { error = "Invalid User name or Password." });
  59. }
  60. public ActionResult Logout()
  61. {
  62. FormsAuthentication.SignOut();
  63. return Redirect(Url.SubRouteUrl("www", "Home.Index"));
  64. }
  65. [HttpGet]
  66. [AllowAnonymous]
  67. // GET: Profile
  68. public ActionResult Register(string ReturnUrl)
  69. {
  70. RegisterViewModel model = new RegisterViewModel();
  71. model.ReturnUrl = ReturnUrl;
  72. return View("/Areas/Profile/Views/Profile/ViewRegistration.cshtml", model);
  73. }
  74. [HttpPost]
  75. [AllowAnonymous]
  76. public ActionResult Register(RegisterViewModel model)
  77. {
  78. if (ModelState.IsValid)
  79. {
  80. var foundUser = db.Users.Where(b => b.Username == model.Username);
  81. if (foundUser.Any())
  82. {
  83. return Json(new { error = "That username already exists." });
  84. }
  85. if (model.Password != model.ConfirmPassword)
  86. {
  87. return Json(new { error = "Passwords must match." });
  88. }
  89. try
  90. {
  91. // Add User
  92. User newUser = db.Users.Create();
  93. newUser.JoinDate = DateTime.Now;
  94. newUser.Username = model.Username;
  95. newUser.HashedPassword = SHA384.Hash(model.Username, model.Password);
  96. db.Users.Add(newUser);
  97. db.SaveChanges();
  98. // Generate blog for the user
  99. var newBlog = db.Blogs.Create();
  100. newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
  101. db.SaveChanges();
  102. }
  103. catch (Exception ex)
  104. {
  105. return Json(new { error = "Unable to create the user." });
  106. }
  107. return Login(new LoginViewModel { Username = model.Username, Password = model.Password, RememberMe = false, ReturnUrl = model.ReturnUrl });
  108. }
  109. return Json(new { error = "You must include all fields." });
  110. }
  111. }
  112. }