Ejemplo n.º 1
0
        public ActionResult <ChangeUsernameModel> ChangeUsername([FromBody] ChangeUsernameModel user)
        {
            IAuthService authService = new JWTService(clientSettings.Value.SecretKey);
            string       token       = HttpContext.Request.Headers["Authorization"];

            try
            {
                if (!authService.IsTokenValid(token))
                {
                    return(BadRequest("Unauthorized Access"));
                }
                else
                {
                    List <Claim> claims = authService.GetTokenClaims(token).ToList();
                    if ((claims.FirstOrDefault(t => t.Type.Equals(ClaimTypes.Name)).Value == user.OldUsername) && userProcessor.ChangeUsername(user.OldUsername, user.NewUsername, user.Password))
                    {
                        return(user);
                    }
                    return(BadRequest("Not valid information"));
                }
            }
            catch
            {
                return(BadRequest("Unauthorized Access"));
            }
        }
Ejemplo n.º 2
0
        public ActionResult ChangeUsername(ChangeUsernameModel changeUsernameModel)
        {
            ViewBag.changeUsernameModel = changeUsernameModel;
            if (User.Identity.IsAuthenticated && ModelState.IsValid)
            {
                var membershipUser = Membership.GetUser();
                var user           = Services.MemberService.GetById((int)membershipUser.ProviderUserKey);
                var allUsernames   = Services.MemberService.GetByUsername(changeUsernameModel.NewUsername);
                if ((changeUsernameModel.NewUsername == changeUsernameModel.ConfirmUsername) && (allUsernames == null))
                {
                    user.Username = changeUsernameModel.NewUsername;
                    Services.MemberService.Save(user);
                    FormsAuthentication.SetAuthCookie(changeUsernameModel.ConfirmUsername, false);
                    TempData["Success"] = "Username successfully changed.";
                    return(RedirectToCurrentUmbracoPage());
                }
                else
                {
                    TempData["Error"] = "Error changing username. Username already exists or New and Confirm Username aren't the same.";
                    return(RedirectToCurrentUmbracoPage());
                }
            }

            return(RedirectToCurrentUmbracoPage());
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> ChangeUsername(ChangeUsernameModel model)
        {
            var username  = _accountRepository.FindById(User.Identity.GetUserId()).UserName;
            var userAsync = await _accountRepository.FindAsync(username, model.Password);

            var allUsernames = _accountRepository.GetUsers().Select(x => x.UserName).ToArray();

            if (allUsernames.Any(x => x.Equals(model.NewUsername)) || userAsync == null)
            {
                if (allUsernames.Any(x => x.Equals(model.NewUsername)))
                {
                    ViewBag.UsernameAlreadyExists = "Username already exists!";
                }
                if (userAsync == null)
                {
                    ViewBag.WrongPassword = "******";
                }
                return(View());
            }

            var result = await _accountRepository.UpdateUsernameAsync(userAsync.Id, model.NewUsername);

            if (result.Succeeded)
            {
                await _accountRepository.UpdateSecurityStampAsync(userAsync.Id);

                userAsync = await _accountRepository.FindAsync(model.NewUsername, model.Password);

                AuthenticationManager.SignOut();
                await _accountRepository.PasswordSignInAsync(userAsync.UserName, model.Password, false, false);
            }

            return(Redirect("Index"));
        }
 public static User UserEntity(ChangeUsernameModel model)
 {
     return(new User
     {
         Username = model.Username,
         Password = model.Password,
         Id = model.Id
     });
 }
Ejemplo n.º 5
0
        public ActionResult ChangeUsername()
        {
            var model = new ChangeUsernameModel()
            {
                NewUsername = _accountRepository.FindById(User.Identity.GetUserId()).UserName,
            };

            return(View(model));
        }
Ejemplo n.º 6
0
        public ActionResult ChangeUsername(ChangeUsernameModel model)
        {
            if (!ModelState.IsValid)
            {
                return(MvcHelper.JsonErrorResult("Model is invalid."));
            }

            // backend update code goes here...

            return(MvcHelper.JsonSuccessResult());
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> ChangeUsername()
        {
            AppUser user = await GetUserAsync();

            var model = new ChangeUsernameModel
            {
                CurrentUsername = user.UserName,
                NewUsername     = user.UserName
            };

            return(View(model));
        }
Ejemplo n.º 8
0
 public MSActorReturnMessageModel ChangeUsername([FromBody] ChangeUsernameModel input)
 {
     try {
         string logmessage = "ChangeUsername | employeeid: " + input.employeeid + " | old_samaccountname: " + input.old_samaccountname +
                             " | new_samaccountname: " + input.new_samaccountname + " | userprincipalname: " + input.userprincipalname;
         util.LogMessage(logmessage);
         ADController control = new ADController();
         return(control.ChangeUsername(input.employeeid, input.old_samaccountname, input.new_samaccountname, input.userprincipalname));
     }
     catch (Exception e)
     {
         return(util.ReportError(e));
     }
 }
Ejemplo n.º 9
0
        public async Task <IActionResult> ChangeUsername(ChangeUsernameModel model)
        {
            if (ModelState.IsValid)
            {
                AppUser user = await GetUserAsync();

                user.UserName = string.IsNullOrEmpty(model.NewUsername) || string.IsNullOrWhiteSpace(model.NewUsername)
                            ? user.Email : model.NewUsername;
                var result = await _userManager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    throw new ApplicationException($"Can not update user. User id: '{_userManager.GetUserId(User)}'");
                }
                return(RedirectToAction("Account", "Manage"));
            }
            return(View(model));
        }
 public IActionResult ChangeUsername(ChangeUsernameModel model)
 {
     ViewBag.header = "Enter your new username";
     if (ModelState.IsValid)
     {
         var user   = ConvertTo.UserEntity(model);
         var status = userService.UpdateUsername(user, model.Username);
         if (status)
         {
             return(RedirectToAction("UserDetails", "User"));
         }
         else
         {
             ModelState.AddModelError(string.Empty, $"Username '{model.Username}' already exists");
             return(View(model));
         }
     }
     return(View(model));
 }
 public async Task <IActionResult> ChangeUsername(ChangeUsernameModel model)
 {
     try
     {
         await _accountService.ChangeUsername(mapper.Map <ChangeUsernameModel, ChangeUsernameDTO>(model));
     }
     catch (ValidationException ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (NotFoundException ex)
     {
         return(NotFound(ex.Message));
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(Ok(new { Message = "Username was successfully changed" }));
 }