Question

Recently I have started using claim-based authentication on an existing web application. Because the application makes use of jQuery & more notably, the AJAX functions, I have had to alter the handlers not to attempt to redirect the XmlHTTPRequests, and instead return a 403 status which is easier to handle.

Here is the FederatedAuthentication.WSFederationAuthenticationModule.AuthorizationFailed event hanlder:

    protected void WSFederationAuthenticationModule_AuthorizationFailed(object sender, AuthorizationFailedEventArgs e)
    {
        //WSFederationAuthenticationModule sam = (WSFederationAuthenticationModule)sender;

        HttpContext context = HttpContext.Current;
        HttpRequest req = context.Request;
        HttpResponse resp = context.Response;

        if (req == null || resp == null) return;

        if ((resp.StatusCode == 302 || resp.StatusCode == 401) && req.Headers["X-Requested-With"] == "XMLHttpRequest")
        {
            resp.StatusCode = 403;
            e.RedirectToIdentityProvider = false;
        }

    }

I have the following pattern that implements the AJAX calls and handle the response:

$.ajax({
cache: false,
data: $.param(o),
dataType: "xml",
url: "AJAXCall.ashx",
success: function (data)
{
    // Success handler
},
error: function (XMLHttpRequest, textStatus, responseText)
{
    if (XMLHttpRequest.status == 403)
    {
        var retVal = window.showModalDialog("Session.aspx", "", "dialogHeight: 250px; dialogWidth: 250px; edge: Raised; center: Yes; resizable: Yes; status: Yes;");
        if (retVal)
        {
            // Succesful session renewal handler
        }
        else
        {
            // Session renewal failure handler
        }
    }
    else
    {
        // Other errors handler
    }
}
});

The 'Session.aspx' basically closes the modal dialog with a return value of true if it successfully redirected to the Identity Provider and back.

But my problem is that I get the following error:

"ID4223: The SamlSecurityToken is rejected because the SamlAssertion.NotOnOrAfter condition is not satisfied."

This is invoked on a subsystem that impersonates the current application user and obviously the token of the previous session still persist. I have the following setting in my application's web.config:

<federatedAuthentication>
<wsFederation passiveRedirectEnabled="true" persistentCookiesOnPassiveRedirects="true" issuer="https://adfs.example.com/adfs/ls/" realm="https://www.example.com:449/" requireHttps="true" />
<cookieHandler requireSsl="true" />

How do I avoid this error? Any help will be greatly appreciated.

Was it helpful?

Solution

The following FederatedAuthentication.WSFederationAuthenticationModule.SignInError event handler method sorted out the problem:

    protected void WSFederationAuthenticationModule_SignInError(object sender, ErrorEventArgs e)
    {
        // handle an intermittent error that most often occurs if you are redirected to the STS after a session expired,
        // and the user clicks back on the browser - second error very uncommon but this should fix
        if (e.Exception.Message.StartsWith("ID4148") ||
            e.Exception.Message.StartsWith("ID4243") ||
            e.Exception.Message.StartsWith("ID4223"))
        {
            FederatedAuthentication.SessionAuthenticationModule.DeleteSessionTokenCookie();
            e.Cancel = true;
        }
    }

It will delete the Session Token Cookie that persisted, even after the user has been redirected to the STS service after a session has expired.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top