예제 #1
0
        public async Task <IActionResult> Logout()
        {
            var token      = Request.GetAccessToken();
            var claims     = HttpContext.User.Claims;
            var id         = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
            var enumerable = await _accountService.GetEntityAsync <AdministratorAccount>(id);

            if (!(enumerable.FirstOrDefault() is AdministratorAccount account))
            {
                await _loggerService.LogInformation("Account not found.");

                return(NotFound());
            }

            var accessToken = new AccessToken {
                Token = token
            };
            var isValid = await _accessTokenService.IsTokenValidAsync(accessToken, id, true);

            if (!isValid)
            {
                throw new SecurityTokenValidationException("Invalid token.");
            }
            var isSuccess = await _accessTokenService.DeleteTokenAsync(accessToken, id, true);

            account.IsOnline = false;
            await _accountService.UpdateEntityAsync(account);

            await _signInManager.SignOutAsync();

            await _loggerService.LogInformation("Admin log out.");

            return(Ok());
        }
        public async Task <IActionResult> Logout()
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var token   = Request.GetAccessToken();
                var claims  = HttpContext.User.Claims;
                var idClaim = claims.FirstOrDefault(c => c.Type == "Id");
                if (idClaim == null)
                {
                    return(null);                     //put something here
                }
                var id         = idClaim.Value;
                var enumerable = await _accountService.GetEntityAsync <UserAccount>(id);

                if (!(enumerable.FirstOrDefault() is UserAccount account))
                {
                    return(null);                                                           //put something here
                }
                var accessToken = new AccessToken {
                    Token = token
                };
                var isSuccess = await _accessTokenService.DeleteTokenAsync(accessToken, id, true);

                account.IsOnline = false;
                await _accountService.UpdateEntityAsync(account);

                await _signInManager.SignOutAsync();

                _logger.LogInformation($"User '{account.Email}' logged out.");
                return(new OkResult());
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(BadRequest(ex.Message));
            }
        }