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