Beispiel #1
0
        public async Task <IActionResult> UpdateLoginInfo(UpdateUserPasswordModel model, string email)
        {
            if (ModelState.IsValid)
            {
                User user = await userManager.FindByEmailAsync(email);

                // Replace current password with new password
                IdentityResult result = await userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);

                if (result.Succeeded)
                {
                    User updatedUser = await userManager.FindByEmailAsync(email);

                    TokenCreatorService tokenService = new TokenCreatorService(_context, _configure);

                    var isAdmin = false;

                    var payload = tokenService.CreateToken(updatedUser, isAdmin, true);

                    return(Ok(payload));
                }
            }

            return(Unauthorized(new APIPayload()));
        }
Beispiel #2
0
        //[Produces("text/plain")]
        public async Task <ActionResult <APIPayload> > LoginUser(LoginModel model)
        {
            // Get user by e-mail
            User user = await _context.Users.Where(x => x.Email == model.UserEmail).FirstOrDefaultAsync();

            if (user == null)
            {
                return(NotFound());
            }

            // Use identity framework to compare passwords in database
            Microsoft.AspNetCore.Identity.SignInResult signInResult = await signInManager.CheckPasswordSignInAsync(user, model.UserPassword, false);

            // If authentication was successful...
            if (signInResult.Succeeded)
            {
                // Is user Admin?
                bool isAdmin = await userManager.IsInRoleAsync(user, "Admin");

                // Construct JWT token
                TokenCreatorService tokenService = new TokenCreatorService(_context, _configure);
                var newToken = tokenService.CreateToken(user, isAdmin, true);

                return(Ok(newToken));
            }

            else
            {
                return(Unauthorized());
            }
        }
Beispiel #3
0
        public async Task <ActionResult <IEnumerable <IdentityError> > > UpdateUserInfo(User user, string email)
        {
            var updateUser = await userManager.FindByNameAsync(email);

            if (updateUser == null)
            {
                return(NotFound());
            }

            // Update user with the new information
            updateUser.UserName      = user.Email;
            updateUser.Email         = user.Email;
            updateUser.FirstName     = user.FirstName;
            updateUser.LastName      = user.LastName;
            updateUser.PhoneNumber   = user.PhoneNumber;
            updateUser.StreetAddress = user.StreetAddress;
            updateUser.ZipCode       = user.ZipCode;
            updateUser.City          = user.City;

            var result = await userManager.UpdateAsync(updateUser);

            if (result.Succeeded)
            {
                TokenCreatorService tokenService = new TokenCreatorService(_context, _configure);

                var isAdmin  = false;
                var newToken = tokenService.CreateToken(updateUser, isAdmin);
                return(Ok(newToken));
            }
            else
            {
                return(BadRequest(result.Errors));
            }
        }
        private async Task <APIPayload> BakeNewToken(User user)
        {
            bool isAdmin = await IsUserAdminAsync(user);

            TokenCreatorService tokenService = new TokenCreatorService(_context, _config);
            var newPayload = tokenService.CreateToken(user, isAdmin);

            return(newPayload);
        }