From ee6f46f44d191c02cb54aa34373a083b1e2d90d8 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Fri, 26 May 2023 23:03:14 +0700 Subject: [PATCH 1/3] fix: logout not working --- src/Api/Controllers/AuthController.cs | 6 ++---- src/Application/Common/Interfaces/IIdentityService.cs | 2 +- src/Infrastructure/Identity/IdentityService.cs | 9 ++++----- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs index da4bef58..8631ea90 100644 --- a/src/Api/Controllers/AuthController.cs +++ b/src/Api/Controllers/AuthController.cs @@ -55,12 +55,10 @@ public async Task Logout() var refreshToken = Request.Cookies[nameof(RefreshToken)]; var jweToken = Request.Cookies["JweToken"]; - var loggedOut = await _identityService.LogoutAsync(jweToken!, refreshToken!); - - if (!loggedOut) return Ok(); - RemoveJweToken(); RemoveRefreshToken(); + + await _identityService.LogoutAsync(jweToken!, refreshToken!); return Ok(); } diff --git a/src/Application/Common/Interfaces/IIdentityService.cs b/src/Application/Common/Interfaces/IIdentityService.cs index 20fab955..048300af 100644 --- a/src/Application/Common/Interfaces/IIdentityService.cs +++ b/src/Application/Common/Interfaces/IIdentityService.cs @@ -8,5 +8,5 @@ public interface IIdentityService Task Validate(string token, string refreshToken); Task RefreshTokenAsync(string token, string refreshToken); Task<(AuthenticationResult AuthResult, UserDto UserCredentials)> LoginAsync(string email, string password); - Task LogoutAsync(string token, string refreshToken); + Task LogoutAsync(string token, string refreshToken); } \ No newline at end of file diff --git a/src/Infrastructure/Identity/IdentityService.cs b/src/Infrastructure/Identity/IdentityService.cs index cb38b55c..9e304486 100644 --- a/src/Infrastructure/Identity/IdentityService.cs +++ b/src/Infrastructure/Identity/IdentityService.cs @@ -1,3 +1,4 @@ +using System.Data; using System.Globalization; using System.IdentityModel.Tokens.Jwt; using System.Security.Authentication; @@ -213,7 +214,7 @@ public async Task RefreshTokenAsync(string token, string r return (await GenerateAuthenticationResultForUserAsync(user), _mapper.Map(user)); } - public async Task LogoutAsync(string token, string refreshToken) + public async Task LogoutAsync(string token, string refreshToken) { var validatedToken = GetPrincipalFromToken(token); @@ -226,7 +227,7 @@ public async Task LogoutAsync(string token, string refreshToken) var storedRefreshToken = await _context.RefreshTokens.SingleOrDefaultAsync(x => x.Token.Equals(Guid.Parse(refreshToken))); - if (storedRefreshToken is null) return true; + if (storedRefreshToken is null) return; if (!storedRefreshToken!.JwtId.Equals(jti)) { @@ -234,9 +235,7 @@ public async Task LogoutAsync(string token, string refreshToken) } _context.RefreshTokens.Remove(storedRefreshToken); - await _context.SaveChangesAsync(); - - return true; + await _context.SaveChangesAsync();; } private async Task GenerateAuthenticationResultForUserAsync(User user) From 83359b32e0a043f64f49f8bb5489c5f0181a1b71 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Fri, 26 May 2023 23:31:17 +0700 Subject: [PATCH 2/3] fix: so is refresh --- src/Api/Controllers/AuthController.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs index 8631ea90..59b65768 100644 --- a/src/Api/Controllers/AuthController.cs +++ b/src/Api/Controllers/AuthController.cs @@ -63,6 +63,7 @@ public async Task Logout() return Ok(); } + [Authorize] [HttpPost] public async Task Refresh() { From 548e0c0219e90a1961ddfb778a1ee45f9a940312 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Sat, 27 May 2023 00:33:09 +0700 Subject: [PATCH 3/3] fix: something --- src/Api/Controllers/AuthController.cs | 4 +--- src/Infrastructure/Identity/IdentityService.cs | 9 ++------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs index 59b65768..b8d8a03c 100644 --- a/src/Api/Controllers/AuthController.cs +++ b/src/Api/Controllers/AuthController.cs @@ -48,13 +48,12 @@ public async Task>> Login([FromBody] LoginModel return Ok(Result.Succeed(loginResult)); } - [Authorize] [HttpPost] public async Task Logout() { var refreshToken = Request.Cookies[nameof(RefreshToken)]; var jweToken = Request.Cookies["JweToken"]; - + RemoveJweToken(); RemoveRefreshToken(); @@ -63,7 +62,6 @@ public async Task Logout() return Ok(); } - [Authorize] [HttpPost] public async Task Refresh() { diff --git a/src/Infrastructure/Identity/IdentityService.cs b/src/Infrastructure/Identity/IdentityService.cs index 9e304486..eff63f43 100644 --- a/src/Infrastructure/Identity/IdentityService.cs +++ b/src/Infrastructure/Identity/IdentityService.cs @@ -186,15 +186,10 @@ public async Task RefreshTokenAsync(string token, string r return principal; } - catch (SecurityTokenExpiredException ex) + catch { return null; } - catch (Exception exception) - { - Console.WriteLine(exception.StackTrace); - return null; - } } public async Task<(AuthenticationResult, UserDto)> LoginAsync(string email, string password) @@ -235,7 +230,7 @@ public async Task LogoutAsync(string token, string refreshToken) } _context.RefreshTokens.Remove(storedRefreshToken); - await _context.SaveChangesAsync();; + await _context.SaveChangesAsync(); } private async Task GenerateAuthenticationResultForUserAsync(User user)