Ejemplo n.º 1
0
        public IActionResult GetUsers( )
        {
            var location = GetControllerActionNames();

            try
            {
                // _logger.LogInfo($"{location}: Attempted Get All Users");
                var users = this.userManager.Users;

                IList <AspUserDTO> operetorList = new List <AspUserDTO>();

                foreach (var u in users)
                {
                    var operatorUser = new AspUserDTO
                    {
                        Id       = u.Id,
                        UserName = u.UserName,
                        Password = u.PasswordHash
                    };

                    operetorList.Add(operatorUser);
                }


                var response = operetorList;
                //this.mapper.Map<IList<AspNetUserDTO>>(users);
                // _logger.LogInfo($"{location}: Sucessfully got all Users");

                return(Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Update([FromBody] AspUserDTO userDTO, string id)
        {
            var location = GetControllerActionNames();

            try
            {
                this.logger.LogWarn($"{location}: user update atempted - id: {id}");
                if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(userDTO.Id))
                {
                    this.logger.LogWarn($"{location}: user  update failed with wrong data");
                    return(BadRequest(ModelState));
                }

                var user = await userManager.FindByIdAsync(id);// .machineRepository.isExists(id);

                if (user == null)
                {
                    this.logger.LogWarn($"{location}: user Data was not found");
                    return(NotFound());
                }
                if (!ModelState.IsValid)
                {
                    this.logger.LogWarn($"{location}: pack Data was Incomplete");
                    return(BadRequest(ModelState));
                }

                user.UserName = userDTO.UserName;
                var isGood = await this.userManager.UpdateAsync(user);

                //  var isGood = await this.machineRepository.Update(machine);
                if (!isGood.Succeeded)
                {
                    return(InternalError($"{location}: user update failed"));
                }

                this.logger.LogInfo($"{location}: user Data with id: {id} was updated");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetUser(string id)
        {
            var location = GetControllerActionNames();

            try
            {
                this.logger.LogInfo($"{location}: Attempted Call for id: {id}");

                //IList<AspUserDTO> operetorList = new List<AspUserDTO>();
                var        users = this.userManager.Users;
                AspUserDTO usr   = null;

                foreach (var uuser in users)
                {
                    if (uuser.Id == id)
                    {
                        usr = new AspUserDTO()
                        {
                            UserName = uuser.UserName,
                            Id       = uuser.Id,
                            Password = uuser.PasswordHash,
                        };
                    }
                }
                //   AspUserDTO user = await userManager.FindByIdAsync(id);
                if (usr == null)
                {
                    this.logger.LogWarn($"{location}: Failed to retrieve record with id: {id}");
                    return(NotFound());
                }


                var response = this.mapper.Map <AspUserDTO>(usr);
                this.logger.LogInfo($"{location}: Successfully got record with id: {id}");
                return(Ok(response));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Login([FromBody] AspUserDTO aspNetUserDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                var userName = aspNetUserDTO.UserName;
                //var password = aspNetUserDTO.Password;
                var password = aspNetUserDTO.Password;// aspNetUserDTO.Password;
                this.logger.LogInfo($"{location}: Login attempted call from user {userName}");

                // var user1 = this.userManager.FindByNameAsync(userName);

                //var res = this.signInManager.UserManager.CheckPasswordAsync(user1.Id, password);

                var result = await signInManager.PasswordSignInAsync(userName.ToString(), "1111", false, false);

                // signInManager.UserManager.CheckPasswordAsync(aspNetUserDTO, aspNetUserDTO.Password);

                if (result.Succeeded)
                {
                    var user = await this.userManager.FindByNameAsync(userName);

                    this.logger.LogInfo($"{location}: {userName} Sucessfully Authenticated");
                    var tokenString = await GenerateJSONWebToken(user);

                    return(Ok(new { token = tokenString }));
                }



                this.logger.LogInfo($"{location}: {userName} Not authenticated");

                return(Unauthorized(aspNetUserDTO));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}:{e.Message} - {e.InnerException}"));
            }
        }