Using ASP.NET Identity 2.1.0,
I'm trying to add a custom Claim so that it gets added to the round-tripping cookie, and not be added to the datastore.
The Claim is for a unique Session Id, unique login, even if for the same UserId (in order to have better auditing of operations done per Session/Client IP address).
The attempt so far is:
Provider = new CookieAuthenticationProvider
{
OnResponseSignIn = (x) =>
{
//Let's pretend this is a Session table Id:
var st = x.Identity.FindFirstValue("ST");
if (string.IsNullOrEmpty(st))
{
//Damn! always needs regeneration because not round-tripping coming back :-(
//Could use Session, but that defeats the purpose of using a cookie...
st = Guid.NewGuid().ToString();
}
x.Identity.AddClaim(new Claim("ST", st));
x.OwinContext.Authentication.SignIn(x.Identity);
},
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromSeconds(6),
regenerateIdentity: async (manager, user) =>
{
var x = await user.GenerateUserIdentityAsync(manager);
return x;
}
)
}
});
using a cache (Session/load balanced Shared/etc.) for the SessionId, using the UserId as the key obviously is not going to work (would return the same SessionId, no matter the ClientIP)
using the UserId + ClientIP as the key would return a SessionId... But ClientIP is notably error prone, so that's a failure waiting to happen.
using a secondary cookie sounds maybe it could work but I'm loath to go creating cookies willy-nilly for a security system without understanding how I would mitigate every single hijacking problem this could bring up....
anybody have a better (hopefully simpler) solution?
What's the class that manages the deserializes the Cookie into an Identity, and back again, and checks whether its still valid? Could I make a custom one, and add a secondary value in there before it is serialized?
Thanks for your help!