Exemple #1
0
 public IActionResult PerformUserLogout()
 {
     if (ModelState.IsValid)
     {
         var token = HttpContext.Request.Headers["Authorization"].ToString().Split(" ")[1];
         if (!string.IsNullOrEmpty(token))
         {
             userTokenPlanner.SetUserTokenPlanner(new UserTokenStrategy(token));
             if (userTokenPlanner.CheckUserTokenIsStillActive())
             {
                 var getLoginKey = HttpContext.User.Claims.Where(c => c.Type == "jti").Select(c => c.Value).FirstOrDefault();
                 userTokenPlanner.UpdateUserTokenDateUpdated();
                 userLoginPlanner.SetUserLoginPlanner(new UserLoginStrategy(new Guid(getLoginKey)));
                 if (userLoginPlanner.UpdateUserLoginLogoutTime())
                 {
                     tokenBlackListPlanner.SetTokenBlackListPlanner(new TokenBlackListStrategy(token, new Guid(getLoginKey)));
                     var isLogout = tokenBlackListPlanner.InsertTokenBlackList();
                     return(Ok(new { logout = isLogout }));
                 }
             }
         }
         return(NoContent());
     }
     return(BadRequest(ModelState));
 }
Exemple #2
0
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            var user = context.HttpContext.User;

            if (user.Identity.IsAuthenticated)
            {
                var request = context.HttpContext.Request;
                var header  = request.Headers["Authorization"].ToString().Split(" ");
                if (header[0] == "Bearer")
                {
                    var loginKey = user.Claims.Where(w => w.Type == "jti").Select(c => c.Value).FirstOrDefault();
                    userTokenPlanner.SetUserTokenPlanner(new UserTokenStrategy(header[1].Trim()));
                    tokenBlackListPlanner.SetTokenBlackListPlanner(new TokenBlackListStrategy(header[1]));
                    if (!userTokenPlanner.CheckUserTokenIsStillActive())
                    {
                        userTokenPlanner.UpdateUserTokenDateUpdated();
                        if (!tokenBlackListPlanner.IsTokenIsBlackListed())
                        {
                            tokenBlackListPlanner.SetTokenBlackListPlanner(new TokenBlackListStrategy(header[1].Trim(), new Guid(loginKey)));
                            tokenBlackListPlanner.InsertTokenBlackList();
                        }
                        context.Result = new UnauthorizedResult();
                        return;
                    }
                }
            }
        }