Skip to content

Instantly share code, notes, and snippets.

@GeorgeTsiokos
Created April 5, 2019 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgeTsiokos/c7c26ff1ab02358158cf59db995c6968 to your computer and use it in GitHub Desktop.
Save GeorgeTsiokos/c7c26ff1ab02358158cf59db995c6968 to your computer and use it in GitHub Desktop.
Use the SessionID for the auth cookie value
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);
}
@GeorgeTsiokos
Copy link
Author

.AddCookie(options =>
{
  options.TicketDataFormat = new SessionIdDataFormat();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment