예제 #1
0
        public ActionResult ChangeUsername(ChangeUsernameViewModel model)
        {
            if (Regex.IsMatch(this.UserProfile.UserName, GlobalConstants.UserNameRegEx)
                && this.UserProfile.UserName.Length >= GlobalConstants.UserNameMinLength
                && this.UserProfile.UserName.Length <= GlobalConstants.UserNameMaxLength)
            {
                return this.RedirectToAction(GlobalConstants.Index, new { controller = "Profile", area = "Users" });
            }

            if (this.ModelState.IsValid)
            {
                if (this.Data.Users.All().Any(x => x.UserName == model.Username))
                {
                    this.ModelState.AddModelError("Username", "This username is not available");
                    return this.View(model);
                }

                this.UserProfile.UserName = model.Username;
                this.Data.SaveChanges();

                this.TempData[GlobalConstants.InfoMessage] = Resources.Account.Views.ChangeUsernameView.Username_changed;
                this.AuthenticationManager.SignOut();
                return this.RedirectToAction("Login", new { controller = "Account", area = string.Empty });
            }

            return this.View(model);
        }
        public ActionResult ChangeUsername(ChangeUsernameViewModel model)
        {
            if (Regex.IsMatch(this.UserProfile.UserName, "^[a-zA-Z]([/._]?[a-zA-Z0-9]+)+$"))
            {
                return this.RedirectToAction("Index", new { controller = "Profile", area = "Users" });
            }

            if (this.ModelState.IsValid)
            {
                if (this.Data.Users.All().Any(x => x.UserName == model.Username))
                {
                    this.ModelState.AddModelError("Username", "This username is not available");
                    return this.View(model);
                }

                this.UserProfile.UserName = model.Username;
                this.Data.SaveChanges();

                this.TempData["InfoMessage"] = "Username was successfully changed. Please log in using your new username.";
                this.AuthenticationManager.SignOut();
                return this.RedirectToAction("Login", new { controller = "Account", area = string.Empty });
            }

            return this.View(model);
        }