The next generation of the Teknik Services. Written in ASP.NET.
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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Security.Cryptography;
  8. using System.Security.Policy;
  9. using System.Text;
  10. using System.Web;
  11. using System.Web.Mvc;
  12. using System.Web.Security;
  13. using SecurityDriven.Inferno.Hash;
  14. using SecurityDriven.Inferno.Mac;
  15. using Teknik.Models;
  16. using Teknik.ViewModels;
  17. using SHA384 = Teknik.Helpers.SHA384;
  18. namespace Teknik.Controllers
  19. {
  20. public class ProfileController : DefaultController
  21. {
  22. private TeknikEntities db = new TeknikEntities();
  23. [AllowAnonymous]
  24. // GET: Profile
  25. public ActionResult Index()
  26. {
  27. return View();
  28. }
  29. [HttpGet]
  30. [AllowAnonymous]
  31. // GET: Profile
  32. public ActionResult Login()
  33. {
  34. return View();
  35. }
  36. [HttpPost]
  37. [AllowAnonymous]
  38. public ActionResult Login(LoginViewModel model)
  39. {
  40. if (ModelState.IsValid)
  41. {
  42. if (model.IsValid())
  43. {
  44. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  45. return Json(new {result = "true"});
  46. }
  47. }
  48. return Json(new { error = "Invalid User name or Password." });
  49. }
  50. public ActionResult Logout()
  51. {
  52. FormsAuthentication.SignOut();
  53. return RedirectToAction("Index", "Home");
  54. }
  55. [HttpPost]
  56. [AllowAnonymous]
  57. public ActionResult Register(RegisterViewModel model)
  58. {
  59. if (ModelState.IsValid)
  60. {
  61. var foundUser = db.Users.Where(b => b.Username == model.Username);
  62. if (foundUser.Any())
  63. {
  64. return Json(new {error = "That username already exists."});
  65. }
  66. if (model.Password != model.ConfirmPassword)
  67. {
  68. return Json(new {error = "Passwords must match."});
  69. }
  70. if (model.Insert())
  71. {
  72. return RedirectToAction("Login", "Profile", new LoginViewModel { Username = model.Username, Password = model.Password });
  73. }
  74. return Json(new { error = "You must include all fields." });
  75. }
  76. return Json(new { error = "You must include all fields." });
  77. }
  78. // GET: Profile/Details/5
  79. public ActionResult Details(int? id)
  80. {
  81. if (ModelState.IsValid)
  82. {
  83. if (id == null)
  84. {
  85. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  86. }
  87. User user = db.Users.Find(id);
  88. if (user == null)
  89. {
  90. return HttpNotFound();
  91. }
  92. return View(user);
  93. }
  94. return HttpNotFound();
  95. }
  96. // GET: Profile/Create
  97. public ActionResult Create()
  98. {
  99. return View();
  100. }
  101. // POST: Profile/Create
  102. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  103. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  104. [HttpPost]
  105. [ValidateAntiForgeryToken]
  106. public ActionResult Create([Bind(Include = "UserId,Username")] User user)
  107. {
  108. if (ModelState.IsValid)
  109. {
  110. db.Users.Add(user);
  111. db.SaveChanges();
  112. return RedirectToAction("Index");
  113. }
  114. return View(user);
  115. }
  116. // GET: Profile/Edit/5
  117. public ActionResult Edit(int? id)
  118. {
  119. if (id == null)
  120. {
  121. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  122. }
  123. User user = db.Users.Find(id);
  124. if (user == null)
  125. {
  126. return HttpNotFound();
  127. }
  128. return View(user);
  129. }
  130. // POST: Profile/Edit/5
  131. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  132. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  133. [HttpPost]
  134. [ValidateAntiForgeryToken]
  135. public ActionResult Edit([Bind(Include = "UserId,Username")] User user)
  136. {
  137. if (ModelState.IsValid)
  138. {
  139. db.Entry(user).State = EntityState.Modified;
  140. db.SaveChanges();
  141. return RedirectToAction("Index");
  142. }
  143. return View(user);
  144. }
  145. // GET: Profile/Delete/5
  146. public ActionResult Delete(int? id)
  147. {
  148. if (id == null)
  149. {
  150. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  151. }
  152. User user = db.Users.Find(id);
  153. if (user == null)
  154. {
  155. return HttpNotFound();
  156. }
  157. return View(user);
  158. }
  159. // POST: Profile/Delete/5
  160. [HttpPost, ActionName("Delete")]
  161. [ValidateAntiForgeryToken]
  162. public ActionResult DeleteConfirmed(int id)
  163. {
  164. User user = db.Users.Find(id);
  165. db.Users.Remove(user);
  166. db.SaveChanges();
  167. return RedirectToAction("Index");
  168. }
  169. protected override void Dispose(bool disposing)
  170. {
  171. if (disposing)
  172. {
  173. db.Dispose();
  174. }
  175. base.Dispose(disposing);
  176. }
  177. }
  178. }