Exemple #1
0
        public async Task <IActionResult> Login([FromBody] UserForLoginDtos userlogin)
        {
            // throw new Exception("Computer says no!");
            if (!string.IsNullOrEmpty(userlogin.Username))
            {
                userlogin.Username = userlogin.Username.ToLower();
            }

            var userAwait = await _repo.Login(userlogin.Username, userlogin.Password);

            if (userAwait == null)
            {
                return(BadRequest("User does not exist!"));
            }

            userlogin.IPAddress = HttpContext.Connection.RemoteIpAddress.ToString();
            _repo.ActivityLog(userAwait.UserID, userlogin.IPAddress);

            //profile info
            // var profile = await _repo.Profile(userAwait.PersonID);
            //generate tokens
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_config.GetSection("AppSettings:token").Value);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, userAwait.UserID.ToString()),
                    new Claim(ClaimTypes.Name, userAwait.PasswordReset.ToString()),
                }),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            return(Ok(tokenString));
        }