pete
pete

Reputation: 25091

Custom HTTPHandler is not getting called

I'm trying to make a HTTPHandler that will authenticate certain static resources, PDF Files in this case.

I've added the following to my web.config:

<configuration>
  <system.web>
    <httpHandlers>
      <clear />
      <add path="*.pdf" verb="*" validate="true" type="AuthenticatedStaticResourceHandler" />
    </httpHandlers>
  </system.web>
</configuration>

Here's my HTTPHandler class:

Imports Microsoft.VisualBasic

Public Class AuthenticatedStaticResourceHandler
    Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim resource As String = String.Empty

        Select Case context.Request.HttpMethod
            Case "GET"
                resource = context.Server.MapPath(context.Request.FilePath)
                If Helpers.User.CanAccessPath(context.Request.FilePath, context.User.Identity.Name) Then
                    SendContentTypeAndFile(context, resource)
                Else
                    FormsAuthentication.RedirectToLoginPage()
                End If
        End Select
    End Sub

    Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

    Private Function SendContentTypeAndFile(ByVal context As HttpContext, ByVal file As String) As HttpContext
        context.Response.ContentType = GetContentType(file)
        context.Response.TransmitFile(file)
        context.Response.End()

        Return context
    End Function

    Private Function GetContentType(ByVal filename As String) As String
        'Used to set the encoding for the reponse stream 
        Dim resource As String = String.Empty
        Dim file As System.IO.FileInfo = New System.IO.FileInfo(filename)

        If file.Exists Then
            Select Case file.Extension.Remove(0, 1).ToLower()
                Case "pdf"
                    resource = "application/pdf"
                Case "jpg"
                    resource = "image/jpg"
                Case "gif"
                    resource = "image/gif"
                Case "png"
                    resource = "image/png"
                Case "css"
                    resource = "text/css"
                Case "js"
                    resource = "text/javascript"
                Case Else
                    resource = String.Empty
            End Select
        End If

        Return IIf(resource.Length > 0, resource, Nothing)
    End Function
End Class

I've set a breakpoint on the Select Case context.Request.HttpMethod line in ProcessRequest, however when I try to access http://localhost/subfolder/subfolder/some.pdf the breakpoint is not triggered. Further, the example PDF I'm attempting to access is buried in a folder that I should not have access to, yet the PDF is served.

This leads me to believe that my HTTPHandler is not being called.

Am I missing something? What am I doing incorrectly?

Upvotes: 0

Views: 1030

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157098

Most likely, you have to add one extra part to your web.config to support all versions of IIS.

For IIS7 and higher, you need to register your handler in the system.webServer section:

<system.webServer>
    <handlers>
        <add ... />
    </handlers>
</system.webServer>

Upvotes: 1

Related Questions