-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ticket #624 : Can end multiple sessions
- Loading branch information
Showing
34 changed files
with
10,280 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
samples/ProtectWebsiteServerside/src/Website/Controllers/BackChannelLogoutController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
|
||
namespace Website.Controllers | ||
{ | ||
public class BackChannelLogoutController : Controller | ||
{ | ||
private readonly IDistributedCache _distributedCache; | ||
|
||
public BackChannelLogoutController(IDistributedCache distributedCache) | ||
{ | ||
_distributedCache = distributedCache; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Logout([FromForm] BackChannelLogoutRequest request) | ||
{ | ||
if (request == null ||string.IsNullOrWhiteSpace(request.LogoutToken)) return BadRequest(); | ||
var logoutToken = request.LogoutToken; | ||
var handler = new JsonWebTokenHandler(); | ||
var jwt = handler.ReadJsonWebToken(request.LogoutToken); | ||
var subject = jwt.Claims.First(c => c.Type == System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames.Sub).Value; | ||
var sessionId = jwt.Claims.First(c => c.Type == System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames.Sid).Value; | ||
await _distributedCache.SetStringAsync($"{subject}_{sessionId}", "disconnected"); | ||
return Ok(); | ||
} | ||
} | ||
|
||
public class BackChannelLogoutRequest | ||
{ | ||
[FromForm(Name = "logout_token")] | ||
public string LogoutToken { get; set; } | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
samples/ProtectWebsiteServerside/src/Website/CustomCookieEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using System.IdentityModel.Tokens.Jwt; | ||
|
||
namespace Website | ||
{ | ||
public class CustomCookieEventHandler : CookieAuthenticationEvents | ||
{ | ||
private readonly IDistributedCache _distributedCache; | ||
|
||
public CustomCookieEventHandler(IDistributedCache distributedCache) | ||
{ | ||
_distributedCache = distributedCache; | ||
} | ||
|
||
public override async Task ValidatePrincipal(CookieValidatePrincipalContext context) | ||
{ | ||
if (context.Principal.Identity.IsAuthenticated) | ||
{ | ||
var subject = context.Principal.FindFirst(JwtRegisteredClaimNames.Sub)?.Value; | ||
var sessionId = context.Principal.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; | ||
var str = await _distributedCache.GetStringAsync($"{subject}_{sessionId}"); | ||
if(!string.IsNullOrWhiteSpace(str)) | ||
{ | ||
context.RejectPrincipal(); | ||
await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.