Example #1
0
        public IActionResult Login([FromBody] EntitiesModels.Users user)
        {
            try
            {
                Users _user = new Users();
                _user = _Context.Users.Where(_use => _use.Username == user.Username && _use.Password == Convert.ToBase64String(Encoding.UTF8.GetBytes(user.Password))).Single();
                if (_user == null)
                {
                    return(BadRequest(new Exception().Message));
                }

                string token;

                if (user.Remember)
                {
                    token = jwtAccess.GenerateAccessToken(_user, 60 * 24 * 30 * 12);//1 year
                }
                else
                {
                    token = jwtAccess.GenerateAccessToken(_user, 60 * 6); // 6 hours
                }

                AccessToken ac = new AccessToken();
                ac.Token  = token;
                ac.Userid = _user.Id;
                _Context.AccessToken.Add(ac);
                _Context.SaveChanges();

                return(Ok(new { Access_Token = token, }));
            }
            catch (Exception e)
            {
                return(StatusCode(404, e.Message));
            }
        }
Example #2
0
        public Users VerifyAccessToken(string Token)
        {
            try{
                EntitiesModels.Users payload = JsonConvert.DeserializeObject <EntitiesModels.Users>(JWT.Decode(Token, secretKey, JwsAlgorithm.HS256));

                if (payload == null || payload.Exp < DateTime.UtcNow)
                {
                    return(null);
                }

                return(payload);
            }catch (Exception ex) {
                throw ex;
            }
        }