Beispiel #1
0
        public async Task <IActionResult> PutUser(int id, UserPassword userPass)
        {
            if (id != userPass.idUser)
            {
                return(BadRequest("User changed is not the same as the user logged in!"));
            }

            userPass.Password = HashMd5Generator.Generate(userPass.Password);

            var user = new User()
            {
                idUser = userPass.idUser, Password = userPass.Password
            };

            _context.Entry(user).Property(x => x.Password).IsModified = true;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                if (!UserExists(id))
                {
                    return(NotFound("User not found!"));
                }
                else
                {
                    return(StatusCode(500, ex.Message));
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult <User> > PostUser(User userData)
        {
            try
            {
                userData.Password = HashMd5Generator.Generate(userData.Password);

                if (UserExistCPF(userData.CPF))
                {
                    return(BadRequest("This CPF as been registered!"));
                }

                _context.User.Add(userData);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.InnerException));
            }

            return(Ok(userData.idUser));
        }
Beispiel #3
0
        public ActionResult <LoginModel> LoginPost(decimal CPF, string Pass,
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
                                                   [FromServices] SigningConfigurations signingConfigurations,
                                                   [FromServices] TokenConfigurations tokenConfigurations)
#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
        {
            User       user       = _context.User.Where(x => x.CPF.Equals(CPF)).FirstOrDefault();
            LoginModel loginModel = new LoginModel();

            if (user == null)
            {
                return(NotFound("User not found with this CPF!"));
            }
            try
            {
                var passCript = HashMd5Generator.Generate(Pass);

                if (user.Password != passCript)
                {
                    return(BadRequest("Passoword is invalid!"));
                }


                bool validCredentials = false;

                validCredentials = (user != null);

                if (validCredentials)
                {
                    ClaimsIdentity identity = new ClaimsIdentity(
                        new GenericIdentity(user.CPF.ToString(), "Login"),
                        new[]
                    {
                        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                        new Claim(JwtRegisteredClaimNames.UniqueName, user.CPF.ToString())
                    }
                        );

                    DateTime creationDate   = DateTime.Now;
                    DateTime expirationDate = creationDate + TimeSpan.FromSeconds(tokenConfigurations.Seconds);

                    var handler = new JwtSecurityTokenHandler();

                    var securityToken = handler.CreateToken(new SecurityTokenDescriptor
                    {
                        Issuer             = tokenConfigurations.Issuer,
                        Audience           = tokenConfigurations.Audience,
                        SigningCredentials = signingConfigurations.SigningCredentials,
                        Subject            = identity,
                        NotBefore          = creationDate,
                        Expires            = expirationDate
                    });

                    var token = handler.WriteToken(securityToken);


                    loginModel.authenticated = true;
                    loginModel.created       = creationDate.ToString("yyyy-MM-dd HH:mm:ss");
                    loginModel.expiration    = expirationDate.ToString("yyyy-MM-dd HH:mm:ss");
                    loginModel.accessToken   = token;
                    loginModel.message       = "OK";

                    return(Ok(loginModel));
                }
                else
                {
                    loginModel.authenticated = false;
                    loginModel.message       = "Failed to authenticate!";

                    return(BadRequest(loginModel));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }