/ComponentOne Samples/Studio for ASP.NET Wijmo/ASP.NET MVC Tools/CS/TahDo/TahDo/Controllers/AccountController.cs

https://github.com/tforsberg/z · C# · 151 lines · 109 code · 23 blank · 19 comment · 12 complexity · 2d802ff9664214c44e9aa56c541daa0e MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Security.Principal;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Web.Routing;
  9. using System.Web.Security;
  10. using TahDo.Models;
  11. namespace TahDo.Controllers
  12. {
  13. public class AccountController : Controller
  14. {
  15. public IFormsAuthenticationService FormsService { get; set; }
  16. public IMembershipService MembershipService { get; set; }
  17. protected override void Initialize(RequestContext requestContext)
  18. {
  19. if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
  20. if (MembershipService == null) { MembershipService = new AccountMembershipService(); }
  21. base.Initialize(requestContext);
  22. }
  23. // **************************************
  24. // URL: /Account/LogOn
  25. // **************************************
  26. public ActionResult LogOn()
  27. {
  28. return View();
  29. }
  30. [HttpPost]
  31. public ActionResult LogOn(LogOnModel model, string returnUrl)
  32. {
  33. if (ModelState.IsValid)
  34. {
  35. if (MembershipService.ValidateUser(model.UserName, model.Password))
  36. {
  37. FormsService.SignIn(model.UserName, model.RememberMe);
  38. if (Url.IsLocalUrl(returnUrl))
  39. {
  40. return Redirect(returnUrl);
  41. }
  42. else
  43. {
  44. return RedirectToAction("Index", "Home");
  45. }
  46. }
  47. else
  48. {
  49. ModelState.AddModelError("", "The user name or password provided is incorrect.");
  50. }
  51. }
  52. // If we got this far, something failed, redisplay form
  53. return View(model);
  54. }
  55. // **************************************
  56. // URL: /Account/LogOff
  57. // **************************************
  58. public ActionResult LogOff()
  59. {
  60. FormsService.SignOut();
  61. return RedirectToAction("Index", "Home");
  62. }
  63. // **************************************
  64. // URL: /Account/Register
  65. // **************************************
  66. public ActionResult Register()
  67. {
  68. ViewBag.PasswordLength = MembershipService.MinPasswordLength;
  69. return View();
  70. }
  71. [HttpPost]
  72. public ActionResult Register(RegisterModel model)
  73. {
  74. if (ModelState.IsValid)
  75. {
  76. // Attempt to register the user
  77. MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
  78. if (createStatus == MembershipCreateStatus.Success)
  79. {
  80. FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
  81. return RedirectToAction("Index", "Home");
  82. }
  83. else
  84. {
  85. ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
  86. }
  87. }
  88. // If we got this far, something failed, redisplay form
  89. ViewBag.PasswordLength = MembershipService.MinPasswordLength;
  90. return View(model);
  91. }
  92. // **************************************
  93. // URL: /Account/ChangePassword
  94. // **************************************
  95. [Authorize]
  96. public ActionResult ChangePassword()
  97. {
  98. ViewBag.PasswordLength = MembershipService.MinPasswordLength;
  99. return View();
  100. }
  101. [Authorize]
  102. [HttpPost]
  103. public ActionResult ChangePassword(ChangePasswordModel model)
  104. {
  105. if (ModelState.IsValid)
  106. {
  107. if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
  108. {
  109. return RedirectToAction("ChangePasswordSuccess");
  110. }
  111. else
  112. {
  113. ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
  114. }
  115. }
  116. // If we got this far, something failed, redisplay form
  117. ViewBag.PasswordLength = MembershipService.MinPasswordLength;
  118. return View(model);
  119. }
  120. // **************************************
  121. // URL: /Account/ChangePasswordSuccess
  122. // **************************************
  123. public ActionResult ChangePasswordSuccess()
  124. {
  125. return View();
  126. }
  127. }
  128. }