/JCE-STD/Controllers/AccountController.cs

https://github.com/stasberkman/JCE-STD · C# · 204 lines · 142 code · 35 blank · 27 comment · 26 complexity · 5bf5903ab990e0d6443c371a6ea5f7a6 MD5 · raw file

  1. /*
  2. account controller includes action result to the pages of the student courses site selection
  3. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Routing;
  10. using System.Web.Security;
  11. using JCE_STD.Models;
  12. namespace JCE_STD.Controllers
  13. {
  14. public class AccountController : Controller
  15. {
  16. //
  17. // GET: /Account/LogOn
  18. public ActionResult LogOn()
  19. {
  20. return View();
  21. }
  22. //
  23. // POST: /Account/LogOn
  24. [HttpPost]
  25. public ActionResult LogOn(LogOnModel model)
  26. {
  27. if (ModelState.IsValid)//manual implementation of the login for the project staff only
  28. {
  29. if (model.UserName == "motiaz" && model.Password == "1234")
  30. {
  31. return RedirectToAction("moti","Home");
  32. }
  33. else if (model.UserName == "stasbe" && model.Password == "1234")
  34. {
  35. return RedirectToAction("stas", "Home");
  36. }
  37. else if (model.UserName == "orenzv" && model.Password == "1234")
  38. {
  39. return RedirectToAction("oren", "Home");
  40. }
  41. else if (model.UserName == "arielle" && model.Password == "1234")
  42. {
  43. return RedirectToAction("ariel", "Home");
  44. }
  45. else
  46. { //wrong pass
  47. ModelState.AddModelError("", "שם המשתמש או הסיסמא שגויים,נסה שנית.");
  48. }
  49. }
  50. else
  51. {
  52. ModelState.AddModelError("", "The user name or password provided is incorrect.");
  53. }
  54. // If we got this far, something failed, redisplay form
  55. return View(model);
  56. }
  57. //
  58. // GET: /Account/LogOff
  59. public ActionResult LogOff()
  60. {
  61. FormsAuthentication.SignOut();
  62. return RedirectToAction("Index", "Home");
  63. }
  64. //
  65. // GET: /Account/Register
  66. public ActionResult Register()
  67. {
  68. return View();
  69. }
  70. //
  71. // POST: /Account/Register
  72. [HttpPost]
  73. public ActionResult Register(RegisterModel model)
  74. {
  75. if (ModelState.IsValid)
  76. {
  77. // Attempt to register the user
  78. MembershipCreateStatus createStatus;
  79. Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
  80. if (createStatus == MembershipCreateStatus.Success)
  81. {
  82. FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
  83. return RedirectToAction("Index", "Home");
  84. }
  85. else
  86. {
  87. ModelState.AddModelError("", ErrorCodeToString(createStatus));
  88. }
  89. }
  90. // If we got this far, something failed, redisplay form
  91. return View(model);
  92. }
  93. //
  94. // GET: /Account/ChangePassword
  95. [Authorize]
  96. public ActionResult ChangePassword()
  97. {
  98. return View();
  99. }
  100. //
  101. // POST: /Account/ChangePassword
  102. [Authorize]
  103. [HttpPost]
  104. public ActionResult ChangePassword(ChangePasswordModel model)
  105. {
  106. if (ModelState.IsValid)
  107. {
  108. // ChangePassword will throw an exception rather
  109. // than return false in certain failure scenarios.
  110. bool changePasswordSucceeded;
  111. try
  112. {
  113. MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
  114. changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
  115. }
  116. catch (Exception)
  117. {
  118. changePasswordSucceeded = false;
  119. }
  120. if (changePasswordSucceeded)
  121. {
  122. return RedirectToAction("ChangePasswordSuccess");
  123. }
  124. else
  125. {
  126. ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
  127. }
  128. }
  129. // If we got this far, something failed, redisplay form
  130. return View(model);
  131. }
  132. //
  133. // GET: /Account/ChangePasswordSuccess
  134. public ActionResult ChangePasswordSuccess()
  135. {
  136. return View();
  137. }
  138. #region Status Codes
  139. private static string ErrorCodeToString(MembershipCreateStatus createStatus)
  140. {
  141. // See http://go.microsoft.com/fwlink/?LinkID=177550 for
  142. // a full list of status codes.
  143. switch (createStatus)
  144. {
  145. case MembershipCreateStatus.DuplicateUserName:
  146. return "User name already exists. Please enter a different user name.";
  147. case MembershipCreateStatus.DuplicateEmail:
  148. return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
  149. case MembershipCreateStatus.InvalidPassword:
  150. return "The password provided is invalid. Please enter a valid password value.";
  151. case MembershipCreateStatus.InvalidEmail:
  152. return "The e-mail address provided is invalid. Please check the value and try again.";
  153. case MembershipCreateStatus.InvalidAnswer:
  154. return "The password retrieval answer provided is invalid. Please check the value and try again.";
  155. case MembershipCreateStatus.InvalidQuestion:
  156. return "The password retrieval question provided is invalid. Please check the value and try again.";
  157. case MembershipCreateStatus.InvalidUserName:
  158. return "The user name provided is invalid. Please check the value and try again.";
  159. case MembershipCreateStatus.ProviderError:
  160. return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  161. case MembershipCreateStatus.UserRejected:
  162. return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  163. default:
  164. return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  165. }
  166. }
  167. #endregion
  168. }
  169. }