Prior to WIF, Application_PostAcquireRequestState
was a good place to create a custom Identity, but required a lot of framework to make sure each type of authentication you were doing was mapped appropriately. By custom Identity, I mean a class inheriting from Identity such as the below SomeIdentity, so that we could have a specific property we might require all authenticated users to have.
PostAcquirerequestState is still available, but there's alot of new ways you can hook into authentication with. Additionally, old methods become complex when supporting multiple authentication methods.
I'd like to know if there's a better way than the below to accomplish this now in WIF. Primarily I'd like to separate out the code that handles mapping claims to the Identity. The idea being that code would be different for other authentication types/providers, as the way it retrieves that property value may not be from a claim such as with SAML, but from somewhere else for other types of authentication methods. I am using Kentor.AuthServices
for SAML support currently. While there might be different code for mapping those values depending on the provider, the end result would be that a SomeIdentity instance had been created and it's SomeProperty and other properties would be set. This way the rest of the application can always depend/assume those have been handled.
My MVC project came with a AccountController
that had a ExternalLoginCallback
which the name implied might be a good hook for when an external authentication completed(which to me SAML is an "external" authentication). However, it does not seem to be hit at any point during/after a SAML authentication.
It may be the answer is that we still need to piece this together ourselves the old way, but I was hoping WIF had some better framework hooks to make this easier.
public sealed class SomeIdentity : Identity
{
...
// Some custom properties
public string SomeProperty { get;set;}
}
protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
...
identity = new SomeIdentity(id, userId);
// map a claim to a specific property
identity.SomeProperty = ...Claims[IdpSomePropertyKey];
///...
GenericPrincipal newPrincipal = new GenericPrincipal(identity , null);
HttpContext.Current.User = newPrincipal;
System.Threading.Thread.CurrentPrincipal = newPrincipal;
}
Now that I'm using WIF, where should I be putting code that is specific to a particular authentication type(i.e. Kentor.AuthServices SAML) which creates a custom SomeIdentity?
The idea being that SomeIdentity would be the identity class used everywhere in my application, but the code to populate it's properties will need to be written specifically for each authentication type, such as with SAML to pull claims and use their values to set the proeprties. I.e. it is where the mapping occurs.