David Archer
David Archer

Reputation: 71

ITfoxtec SAML 2.0 and ASP.NET Web Forms (ASPX) HttpRequest

I am trying to implement ITfoxtec SAML 2.0 in an ASP.NET Web Forms (ASPX) application.

The usual ASP.NET MVC implementation uses the statement:

binding.ReadSamlResponse(Request.ToGenericHttpRequest(), saml2AuthnResponse);

but the MVC Request is not available in Web Forms.

In VB I have tried code such as

Dim request As New ITfoxtec.Identity.Saml2.Http.HttpRequest
request.Method = "GET"
request.Query = HttpContext.Current.Request.Form

binding.ReadSamlResponse(request, saml2AuthnResponse)

but this raises the exception:

Found invalid data while decoding.
at System.IO.Compression.InflaterZlib.Inflate(FlushCode flushCode)
at System.IO.Compression.InflaterZlib.ReadInflateOutput(Byte[] outputBuffer, Int32 offset, Int32 length, FlushCode flushCode, Int32& bytesRead)
at System.IO.Compression.InflaterZlib.Inflate(Byte[] bytes, Int32 offset, Int32 length)
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize)
at System.IO.Stream.CopyTo(Stream destination)
at ITfoxtec.Identity.Saml2.Saml2RedirectBinding.DecompressResponse(String value)
at ITfoxtec.Identity.Saml2.Saml2RedirectBinding.Read(HttpRequest request, Saml2Request saml2RequestResponse, String messageName, Boolean validateXmlSignature, Boolean detectReplayedTokens)
at ITfoxtec.Identity.Saml2.Saml2Binding`1.ReadSamlResponse(HttpRequest request, Saml2Response saml2Response)
at [my code]

How can I convert HttpContext.Current.Request to ITfoxtec.Identity.Saml2.Http.HttpRequest?

Hoping for assistance,

David.

Upvotes: 2

Views: 1142

Answers (2)

David Archer
David Archer

Reputation: 71

Thank you Anders.

The issue I had occurred because I had inadvertantly declared binding as Saml2RedirectBinding instead of Saml2PostBinding. With this corrected, the ASP.NET MVC C# statement:

binding.ReadSamlResponse(Request.ToGenericHttpRequest(), saml2AuthnResponse);

needs to be as follows for ASP.NET Web Forms in VB:

Dim request As New ITfoxtec.Identity.Saml2.Http.HttpRequest With
{
 .Method = HttpContext.Current.Request.HttpMethod,
 .Form = HttpContext.Current.Request.Form,
 .Query = New NameValueCollection,
 .QueryString = ""
}
binding.ReadSamlResponse(request, saml2AuthnResponse)

in case anyone else needs this solution.

David.

Upvotes: 2

Anders Revsgaard
Anders Revsgaard

Reputation: 4334

You probably need to convert the HttpContext.Current.Request.QueryString into a new list.

Maybe something like this:

public static class HttpRequestExtensions
{
    public static Http.HttpRequest ToGenericHttpRequest(this HttpRequest request)
    {
        return new Http.HttpRequest
        {
            Method = "GET",
            QueryString = ToQueryString(HttpContext.Current.Request.RawUrl),
            Query = ToNameValueCollection(HttpContext.Current.Request.QueryString)                
        };
    }

    private static string ToQueryString(string rawUrl)
    {
        // TODO Return the queryString....
        return queryString;
    }

    private static NameValueCollection ToNameValueCollection(??? queryString)
    {
        var nv = new NameValueCollection();
        foreach (var item in queryString)
        {
            nv.Add(item.Key, item.Value.First());
        }
        return nv;
    }
}

Code from: https://github.com/ITfoxtec/ITfoxtec.Identity.Saml2/blob/master/src/ITfoxtec.Identity.Saml2.MvcCore/Extensions/HttpRequestExtensions.cs

Upvotes: 0

Related Questions