Пример #1
0
        //[Authorize]
        public async Task <ActionResult <SaveUserResource> > UpdateUser(SaveUserResource saveUserResource)
        {
            /// validation
            var validation       = new SaveUserResourceValidation();
            var validationResult = await validation.ValidateAsync(saveUserResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            // si la ServiceItem existe depuis le id
            var userUpdate = await _serviceUser.GetById(saveUserResource.Id);

            if (userUpdate == null)
            {
                return(BadRequest("User n'existe pas "));
            }

            string password = saveUserResource.Password;

            var user = _mapper.Map <SaveUserResource, User>(saveUserResource);

            // mappage
            _serviceUser.Update(user, saveUserResource.Password);

            var userUpdateNew = await _serviceUser.GetById(user.Id);

            var userResourceUpdate = _mapper.Map <User, SaveUserResource>(userUpdateNew);

            return(Ok(userResourceUpdate));
        }
Пример #2
0
        public async Task <ActionResult <UserResource> > Register(UserResource userResource)
        {
            // Validation
            var validation       = new SaveUserResourceValidation();
            var validationResult = await validation.ValidateAsync(userResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            // Mappage
            var user     = _mapper.Map <UserResource, User>(userResource);
            var userSave = await _userService.Create(user, userResource.Password);

            // Send Token
            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(_config.GetValue <string>("AppSettings:SecretKey")); // Not optimal

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256)
            };

            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            return(Ok(new
            {
                Id = user.Id,
                Username = user.UserName,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Token = tokenString
            }));
        }