如何使用 ImageResizer 插件添加 Access-Control-Allow-Origin header

How to add Access-Control-Allow-Origin header with an ImageResizer plugin

ImageResizer 文档中的扩展点页面提到插件可以 "Modify http headers" 但他们没有任何说明或示例说明如何这样做。我创建了一个实现 IPluginIChache 接口的插件来添加 HTTP header 但感觉不对,因为:

  1. 我不想实施或创建 "an alternate caching system"
  2. 如果我不在 Process() 方法末尾包含代码 current.RemapHandler(New NoCacheHandler(e)),它就不起作用(图像不会 display/displays 作为空白图像)。我不明白这条线在做什么或重新映射处理程序的含义

这是我当前的实现

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    ...
    <resizer>
        <plugins>
            <add name="MyNamespace.CorsHeadersPlugin" />
            <add name="Watermark" />
            <add name="MvcRoutingShim" />
            <add name="PsdReader" />
            <add name="WicDecoder" />
            <add name="WicEncoder" />
            <add name="WicBuilder" />
        </plugins>
    </resizer>
    ...
</configuration>

CorsHeadersPlugin

Imports ImageResizer.Caching
Imports ImageResizer.Configuration
Imports ImageResizer.Plugins
Imports ImageResizer.Plugins.Basic

Public Class CorsHeadersPlugin
    Implements IPlugin
    Implements ICache

    Private ReadOnly AllowedDomains As String()

    Public Sub New()
        AllowedDomains = {"http://allowthisdomain.com"}

#If DEBUG Then
        AllowedDomains = AllowedDomains.Union({"http://localhost", "https://localhost"}).ToArray
#End If
    End Sub

    Public Function CanProcess(current As HttpContext, e As IResponseArgs) As Boolean Implements ICache.CanProcess
        Return True
    End Function

    Public Sub Process(current As HttpContext, e As IResponseArgs) Implements ICache.Process
        Dim origin = current.Request.Url.GetLeftPart(UriPartial.Authority)

        If AllowedDomains.Contains(origin) Then
            current.Response.Headers.Add("Access-Control-Allow-Origin", origin)
        End If

        current.RemapHandler(New NoCacheHandler(e))
    End Sub

    Public Function Install(c As Config) As IPlugin Implements IPlugin.Install
        c.Plugins.add_plugin(Me)
        Return Me
    End Function

    Public Function Uninstall(c As Config) As Boolean Implements IPlugin.Uninstall
        c.Plugins.remove_plugin(Me)
        Return True
    End Function
End Class

事件处理程序 ImageResizer.Config.Current.Pipeline.PreHandleImage and .PreHandleImageAsync 允许您修改响应 headers。确保在 AppStart 期间只注册一次处理程序。

更新,工作实施

Imports ImageResizer.Caching
Imports ImageResizer.Configuration
Imports ImageResizer.Plugins

Public Class CorsHeadersPlugin
    Implements IPlugin

    Private ReadOnly AllowedDomains As String()

    Public Sub New()
        AllowedDomains = {"http://allowthisdomain.com"}

#If DEBUG Then
        AllowedDomains = AllowedDomains.Union({"http://localhost:3416", "https://localhost:3416"}).ToArray
#End If
    End Sub

    Public Function Install(c As Config) As IPlugin Implements IPlugin.Install
        AddHandler c.Pipeline.PreHandleImage, AddressOf ModifyHttpHeaders
        c.Plugins.add_plugin(Me)
        Return Me
    End Function

    Public Function Uninstall(c As Config) As Boolean Implements IPlugin.Uninstall
        RemoveHandler c.Pipeline.PreHandleImage, AddressOf ModifyHttpHeaders
        c.Plugins.remove_plugin(Me)
        Return True
    End Function

    Private Sub ModifyHttpHeaders(sender As IHttpModule, context As HttpContext, e As IResponseArgs)
        Dim origin = GetRequestOrigin(context.Request)

        If origin IsNot Nothing AndAlso AllowedDomains.Contains(origin) Then
            context.Response.Headers.Add("Access-Control-Allow-Origin", origin)
        End If
    End Sub

    Private Function GetRequestOrigin(request As HttpRequest) As String
        If request.Headers("Origin") IsNot Nothing Then
            Return request.Headers("Origin")
        ElseIf request.UrlReferrer IsNot Nothing Then
            Return request.UrlReferrer.GetLeftPart(UriPartial.Authority)
        End If

        Return Nothing
    End Function
End Class