There are several places where you can do this, but if you use the SessionAuthenticationModule , some of the processes are not well documented, which can be done using a custom main element. The rest of this answer explains one possible way to handle this when using the SessionAuthenticationModule .
Cancel the SessionAuthenticationModule.SetPrincipalFromSessionToken method.
The SessionAuthenticationModule module stores the security token, principal, identifiers and claims in a cookie and cache in memory to avoid the need to make circular trips to the identification / token service with each request. What is not well documented is having a cache and checking its first place, then a cookie and restrictions on serializing ClaimsPrincipal .
If you have already set the user principle in ClaimsAuthenticationManager.Authenticate and the cache is intact, your user manager will most likely be there, since the cache stores its own .NET objects. If you have not yet set up a custom principle or the cache is not full, then the security token will be retrieved from the FedAuth session cookie.
When a token is serialized / deserialized to / from a cookie, the process uses custom serialization, which is able to read and write attributes of the IClaimsPrincipal and IClaimsIdentity (or the ClaimsPrinicpal and ClaimsIdentity classes - I donβt remember which one). Any custom attributes of the main and identification objects will not be included. It may be possible to override serialization, but this requires several (3 IIRC) more layers of class override.
You also need to know that the basic SetPrincipalFromSessionToken method creates a new ClaimsPrincipal object and sets it in the stream and context, so even if the sessionSecurityToken parameter contains a user-defined main object, it will be transferred back to the ClaimsPrincipal object.
Here is an example of an override method:
protected override void SetPrincipalFromSessionToken(SessionSecurityToken sessionSecurityToken) { SessionSecurityToken newToken = MyClaimsPrincipalUtility.CreateCustomClaimsPrincipalToken(sessionSecurityToken); base.SetPrincipalFromSessionToken(newToken);
The implementation of the base class ( SessionAuthenticationModule ) is as follows. Thus, there are several different ways that you can achieve redefinition and get the basic principle of assignment.
protected virtual void SetPrincipalFromSessionToken(SessionSecurityToken sessionSecurityToken) { IClaimsPrincipal fromIdentities = ClaimsPrincipal.CreateFromIdentities(this.ValidateSessionToken(sessionSecurityToken)); HttpContext.Current.User = (IPrincipal) fromIdentities; Thread.CurrentPrincipal = (IPrincipal) fromIdentities;
And just in case you are interested, here is the base class implementation for SessionAuthenticationModule.ContextSessionSecurityToken .
public virtual SessionSecurityToken ContextSessionSecurityToken { get { return (SessionSecurityToken) HttpContext.Current.Items[(object) typeof (SessionSecurityToken).AssemblyQualifiedName]; } internal set { HttpContext.Current.Items[(object) typeof (SessionSecurityToken).AssemblyQualifiedName] = (object) value; } }