Created
April 5, 2019 01:34
-
-
Save GeorgeTsiokos/c7c26ff1ab02358158cf59db995c6968 to your computer and use it in GitHub Desktop.
Use the SessionID for the auth cookie value
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
public sealed class SessionIdDataFormat : ISecureDataFormat<AuthenticationTicket> | |
{ | |
private const string SessionIdClaim = "Microsoft.AspNetCore.Authentication.Cookies-SessionId"; | |
public string Protect(AuthenticationTicket data) => data.Principal.FindFirst(SessionIdClaim).Value; | |
public string Protect(AuthenticationTicket data, string purpose) => null != purpose ? null : Protect(data); | |
public AuthenticationTicket Unprotect(string protectedText) | |
{ | |
var claimsPrincipal = new ClaimsPrincipal(); | |
var claimsIdentity = new ClaimsIdentity(); | |
claimsIdentity.AddClaim(new Claim(SessionIdClaim, protectedText)); | |
claimsPrincipal.AddIdentity(claimsIdentity); | |
return new AuthenticationTicket(claimsPrincipal, CookieAuthenticationDefaults.AuthenticationScheme); | |
} | |
public AuthenticationTicket Unprotect(string protectedText, string purpose) => null != purpose ? null : Unprotect(protectedText); | |
} |
Author
GeorgeTsiokos
commented
Apr 5, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment