PageRenderTime 42ms CodeModel.GetById 18ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/MvcMusicStore/Controllers/AccountController.cs

#
C# | 207 lines | 194 code | 8 blank | 5 comment | 8 complexity | cec1f3593e69f5d99ae78dda4240b5e0 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Web;
  5using System.Web.Mvc;
  6using System.Web.Routing;
  7using System.Web.Security;
  8using Mvc3ToolsUpdateWeb_Default.Models;
  9using MvcMusicStore.Models;
 10
 11namespace Mvc3ToolsUpdateWeb_Default.Controllers
 12{
 13    public class AccountController : Controller
 14    {
 15
 16        private void MigrateShoppingCart(string UserName)
 17        {
 18            // Associate shopping cart items with logged-in user
 19            var cart = ShoppingCart.GetCart(this.HttpContext);
 20
 21            cart.MigrateCart(UserName);
 22            Session[ShoppingCart.CartSessionKey] = UserName;
 23        }
 24
 25        //
 26        // GET: /Account/LogOn
 27
 28        public ActionResult LogOn()
 29        {
 30            return View();
 31        }
 32
 33        //
 34        // POST: /Account/LogOn
 35
 36        [HttpPost]
 37        public ActionResult LogOn(LogOnModel model, string returnUrl)
 38        {
 39            if (ModelState.IsValid)
 40            {
 41                if (Membership.ValidateUser(model.UserName, model.Password))
 42                {
 43                    MigrateShoppingCart(model.UserName); 
 44                    
 45                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
 46                    if (Url.IsLocalUrl(returnUrl) && 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                else
 57                {
 58                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
 59                }
 60            }
 61
 62            // If we got this far, something failed, redisplay form
 63            return View(model);
 64        }
 65
 66        //
 67        // GET: /Account/LogOff
 68
 69        public ActionResult LogOff()
 70        {
 71            FormsAuthentication.SignOut();
 72
 73            return RedirectToAction("Index", "Home");
 74        }
 75
 76        //
 77        // GET: /Account/Register
 78
 79        public ActionResult Register()
 80        {
 81            return View();
 82        }
 83
 84        //
 85        // POST: /Account/Register
 86
 87        [HttpPost]
 88        public ActionResult Register(RegisterModel model)
 89        {
 90            if (ModelState.IsValid)
 91            {
 92                // Attempt to register the user
 93                MembershipCreateStatus createStatus;
 94                Membership.CreateUser(model.UserName, model.Password, model.Email, "question", "answer", true, null, out createStatus);
 95
 96                if (createStatus == MembershipCreateStatus.Success)
 97                {
 98                    MigrateShoppingCart(model.UserName); 
 99                    
100                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
101                    return RedirectToAction("Index", "Home");
102                }
103                else
104                {
105                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
106                }
107            }
108
109            // If we got this far, something failed, redisplay form
110            return View(model);
111        }
112
113        //
114        // GET: /Account/ChangePassword
115
116        [Authorize]
117        public ActionResult ChangePassword()
118        {
119            return View();
120        }
121
122        //
123        // POST: /Account/ChangePassword
124
125        [Authorize]
126        [HttpPost]
127        public ActionResult ChangePassword(ChangePasswordModel model)
128        {
129            if (ModelState.IsValid)
130            {
131
132                // ChangePassword will throw an exception rather
133                // than return false in certain failure scenarios.
134                bool changePasswordSucceeded;
135                try
136                {
137                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
138                    changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
139                }
140                catch (Exception)
141                {
142                    changePasswordSucceeded = false;
143                }
144
145                if (changePasswordSucceeded)
146                {
147                    return RedirectToAction("ChangePasswordSuccess");
148                }
149                else
150                {
151                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
152                }
153            }
154
155            // If we got this far, something failed, redisplay form
156            return View(model);
157        }
158
159        //
160        // GET: /Account/ChangePasswordSuccess
161
162        public ActionResult ChangePasswordSuccess()
163        {
164            return View();
165        }
166
167        #region Status Codes
168        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
169        {
170            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
171            // a full list of status codes.
172            switch (createStatus)
173            {
174                case MembershipCreateStatus.DuplicateUserName:
175                    return "User name already exists. Please enter a different user name.";
176
177                case MembershipCreateStatus.DuplicateEmail:
178                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
179
180                case MembershipCreateStatus.InvalidPassword:
181                    return "The password provided is invalid. Please enter a valid password value.";
182
183                case MembershipCreateStatus.InvalidEmail:
184                    return "The e-mail address provided is invalid. Please check the value and try again.";
185
186                case MembershipCreateStatus.InvalidAnswer:
187                    return "The password retrieval answer provided is invalid. Please check the value and try again.";
188
189                case MembershipCreateStatus.InvalidQuestion:
190                    return "The password retrieval question provided is invalid. Please check the value and try again.";
191
192                case MembershipCreateStatus.InvalidUserName:
193                    return "The user name provided is invalid. Please check the value and try again.";
194
195                case MembershipCreateStatus.ProviderError:
196                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
197
198                case MembershipCreateStatus.UserRejected:
199                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
200
201                default:
202                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
203            }
204        }
205        #endregion
206    }
207}