PageRenderTime 96ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ITELCA_CONTROLESTUDIOS/Controllers/AccountController.cs

https://gitlab.com/oscarsalazarsevilla/ITELCA
C# | 273 lines | 253 code | 7 blank | 13 comment | 9 complexity | 7a5209693daa4a4e31acc6fcd1180094 MD5 | raw file
  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.Routing;
  7. using System.Web.Security;
  8. using ITELCA_CONTROLESTUDIOS.Models;
  9. using ITELCA_CLASSLIBRARY.Models.Classes;
  10. using ITELCA_CLASSLIBRARY.Models;
  11. using ITELCA_CLASSLIBRARY.Services;
  12. using System.Web.SessionState;
  13. namespace ITELCA_CONTROLESTUDIOS.Controllers
  14. {
  15. public class AccountController : Controller
  16. {
  17. //
  18. // GET: /Account/LogOn
  19. public ActionResult LogOn()
  20. {
  21. return View();
  22. }
  23. //
  24. // POST: /Account/LogOn
  25. [HttpPost]
  26. public ActionResult LogOn(LogOnModel model, string returnUrl)
  27. {
  28. if (ModelState.IsValid)
  29. {
  30. if (Membership.ValidateUser(model.UserName, model.Password))
  31. {
  32. //MembershipUser user = Membership.GetUser(model.UserName, true);
  33. //if (user != null)
  34. //{
  35. // if (user.LastActivityDate > DateTime.Now.Subtract(new TimeSpan(0, Membership.UserIsOnlineTimeWindow, 0)))
  36. // {
  37. // ModelState.AddModelError("", "Existe Una session Abierta");
  38. // }
  39. // else
  40. // {
  41. FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
  42. Session["InicioSesion"] = "Si";
  43. Session["UsuarioClaveIncorrecta"] = "No";
  44. Session["IdUsuario"] = new ServicioUsuario().ObtenerPorLogin(model.UserName.ToString()).USUARIO_ID.ToString();
  45. Session["NombreUsuario"] = new ServicioUsuario().ObtenerPorLogin(model.UserName.ToString()).NOMBREUSUARIO.ToString();
  46. if (returnUrl != null && returnUrl.Length > 1 && returnUrl.StartsWith("/")
  47. && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
  48. {
  49. return Redirect(returnUrl);
  50. }
  51. else
  52. {
  53. return RedirectToAction("Index", "Home");
  54. }
  55. // }
  56. //}
  57. }
  58. else
  59. {
  60. Session["UsuarioClaveIncorrecta"] = "Si";
  61. return RedirectToAction("Index", "Home");
  62. // ModelState.AddModelError("", "Usuario O clave Incorrecta");
  63. }
  64. }
  65. // If we got this far, something failed, redisplay form
  66. return View(model);
  67. }
  68. //
  69. // GET: /Account/LogOff
  70. public ActionResult LogOff()
  71. {
  72. MembershipUser user = Membership.GetUser(User.Identity.Name, true);
  73. Membership.UpdateUser(user);
  74. FormsAuthentication.SignOut();
  75. Session.Clear();
  76. return RedirectToAction("LogOn", "Account");
  77. }
  78. //
  79. // GET: /Account/Register
  80. public ActionResult Register()
  81. {
  82. return View();
  83. }
  84. //
  85. // POST: /Account/Register
  86. [HttpPost]
  87. public ActionResult Register(RegisterModel model)
  88. {
  89. if (ModelState.IsValid)
  90. {
  91. // Attempt to register the user
  92. MembershipCreateStatus createStatus;
  93. Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
  94. if (createStatus == MembershipCreateStatus.Success)
  95. {
  96. FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
  97. return RedirectToAction("Index", "Home");
  98. }
  99. else
  100. {
  101. ModelState.AddModelError("", ErrorCodeToString(createStatus));
  102. }
  103. }
  104. // If we got this far, something failed, redisplay form
  105. return View(model);
  106. }
  107. //
  108. // GET: /Account/ChangePassword
  109. [Authorize]
  110. public ActionResult ChangePassword()
  111. {
  112. return View();
  113. }
  114. //
  115. // POST: /Account/ChangePassword
  116. [Authorize]
  117. [HttpPost]
  118. public ActionResult ChangePassword(ChangePasswordModel model)
  119. {
  120. if (ModelState.IsValid)
  121. {
  122. // ChangePassword will throw an exception rather
  123. // than return false in certain failure scenarios.
  124. bool changePasswordSucceeded;
  125. try
  126. {
  127. MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
  128. changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
  129. }
  130. catch (Exception)
  131. {
  132. changePasswordSucceeded = false;
  133. }
  134. if (changePasswordSucceeded)
  135. {
  136. return RedirectToAction("ChangePasswordSuccess");
  137. }
  138. else
  139. {
  140. ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
  141. }
  142. }
  143. // If we got this far, something failed, redisplay form
  144. return View(model);
  145. }
  146. public ActionResult ResetPassword()
  147. {
  148. return View();
  149. }
  150. public ActionResult PasswordResetSuccessful()
  151. {
  152. return View();
  153. }
  154. [HttpPost]
  155. public ActionResult ResetPassword(RecoverPasswordModel model)
  156. {
  157. if (ModelState.IsValid)
  158. {
  159. try
  160. {
  161. MembershipUser user = Membership.GetUser(model.UserName);
  162. string newPassword = user.ResetPassword();
  163. EmailBroadcaster.SendEmail("Su nueva contraseña para ingresar al Sistema", "Su nueva contraseña para ingresar al Sistema es: " + newPassword, user.Email);
  164. Session.Add("Mensaje", "Una nueva contraseña ha sido enviada a su correo exitosamente.");
  165. return RedirectToAction("PasswordResetSuccessful", "Account");
  166. }
  167. catch
  168. {
  169. ModelState.AddModelError("Nombre de usuario", new Exception());
  170. Session.Add("Mensaje", "Ocurrió un error al intentar reiniciar la contraseña.");
  171. return RedirectToAction("Index", "Home");
  172. }
  173. }
  174. return View(model);
  175. }
  176. //
  177. // GET: /Account/ChangePasswordSuccess
  178. public ActionResult ChangePasswordSuccess()
  179. {
  180. return View();
  181. }
  182. [HttpPost]
  183. public ActionResult LogOnHome(string username, string password, string returnUrl)
  184. {
  185. LogOnModel model = new LogOnModel();
  186. model.UserName = username;
  187. model.Password = password;
  188. return this.LogOn(model, returnUrl);
  189. }
  190. public ActionResult LogOffHome()
  191. {
  192. MembershipUser user = Membership.GetUser(User.Identity.Name, true);
  193. Membership.UpdateUser(user);
  194. FormsAuthentication.SignOut();
  195. Session.Clear();
  196. return RedirectToAction("Index", "Home");
  197. }
  198. #region Status Codes
  199. private static string ErrorCodeToString(MembershipCreateStatus createStatus)
  200. {
  201. // See http://go.microsoft.com/fwlink/?LinkID=177550 for
  202. // a full list of status codes.
  203. switch (createStatus)
  204. {
  205. case MembershipCreateStatus.DuplicateUserName:
  206. return "User name already exists. Please enter a different user name.";
  207. case MembershipCreateStatus.DuplicateEmail:
  208. return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
  209. case MembershipCreateStatus.InvalidPassword:
  210. return "The password provided is invalid. Please enter a valid password value.";
  211. case MembershipCreateStatus.InvalidEmail:
  212. return "The e-mail address provided is invalid. Please check the value and try again.";
  213. case MembershipCreateStatus.InvalidAnswer:
  214. return "The password retrieval answer provided is invalid. Please check the value and try again.";
  215. case MembershipCreateStatus.InvalidQuestion:
  216. return "The password retrieval question provided is invalid. Please check the value and try again.";
  217. case MembershipCreateStatus.InvalidUserName:
  218. return "The user name provided is invalid. Please check the value and try again.";
  219. case MembershipCreateStatus.ProviderError:
  220. return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  221. case MembershipCreateStatus.UserRejected:
  222. return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  223. default:
  224. return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  225. }
  226. }
  227. #endregion
  228. }
  229. }