The great “A Guide to Claims-Based Identity and Access Control” handbook shows how to integrate WIF into a MVC application. They propose a new custom MVC filter to replace the built-in Authorize filter and they called the new filter AuthenticateAndAuthorize. The new filter is responsible for redirecting unauthenticated requests to a STS and then they need a dedicated view (FederatedResult) to get and translate the response of the STS.
I believe an easier approach exists – where you don’t have to introduce your own authorization filter and (possibly) don’t have to correct all your controllers so that they are decorated with the new AuthenticateAndAuthorize filter instead of the existing Authorize. This easier approach consist in using <wif:FederatedPassiveSignIn> which is commonly used in ASP.NET WebForms applications.
To be able to use the built-in control, you have to make sure that your existing login view uses ASPX engine as it’s probably impossible to use the WIF’s control in a razor view. But of course this is not an issue, since in MVC you can choose engines separately for each view.
So if your web.config looks like this
and the LoginController
then you replace the existing LoginPage.cshtml with LoginPage.aspx (an ASPX instead of Razor view) and put this into the view:
And yes, this is just as simple.
Note that I need to set the Issuer (the address of the STS) (I read it from app settings) and the Realm (the address STS returns to) (I set it to the LoginPage [current] action).
What happens now is when the Authorize filter redirects requests to the LoginPage action on the Login controller, the FederatedPassiveSignIn control automatically redirects (AutoSignIn=”true”) to the STS and then when the response comes back from the STS the control picks it up and creates the WIF cookie.
Remember of course that for this to work you have to enable the SessionAuthenticationModule in your web.config:
Another trick is to use a slightly modified version of request validation routine. Normally we validate SAML responses like this:
However, it doesn’t work in MVC causing the stack to overflow as MVC 3 seems to utilize a new, lazy method of input validation (frankly, I’ve read the article two or three times and still don’t think I understand the whole point). The modified validator must then make sure it refers to unvalidated values from the request:
This was not obvious and I have finally found a clue at Technet (it is interesting however that the Technet page suggests that the modified validator is presented somewhere in WIF SDK but I cannot confirm it is).
Anyway, replacing the request validator and using FederatedPassiveSignIn on my ASPX view seems to be enough to integrate WIF and MVC 3.