如何使用 CefSharp 的自定义方案将 'Access-Control-Allow-Origin' 添加到请求的资源

How do I add a 'Access-Control-Allow-Origin' to a requested resource with a custom scheme with CefSharp

使用带有自定义方案的 我正在加载本地文件。为了测试,我使用了 disable-web-security 参数,但我不想使用禁用此功能的浏览器。

启用 disable-web-security 后,我会得到

No 'Access-Control-Allow-Origin' header is present on the requested resource.

如何(或在哪里)将其添加到 header 或以其他方式修复它。

首先,我尝试使用 AddCrossOriginWhitelistEntry,但我不知道要输入什么。我的方案是 test://,我调用例如 test://local/folder/3Markets.xlsx。网页来源是https://product.company.de

CefSharp.Cef.AddCrossOriginWhitelistEntry("test://local", "test", "product.company.de", True)

还有这个

CefSharp.Cef.AddCrossOriginWhitelistEntry("test://local", "test", "", True)

但老实说,我不明白我应该放在这里的是什么。

我还认为我可以通过自定义方案处理来解决这个问题。

Dim cs As New CefCustomScheme With {
        .SchemeName = DBSchemeHandler.DBSchemeName,
        .SchemeHandlerFactory = New DBSchemeHandler,
        .IsStandard = True,   'DONT THINK THIS IS NEEDED
        .IsCorsEnabled = True   'DONT THINK THIS IS NEEDED
    }
    dbSurferSettings.RegisterScheme(cs)

我想在回复中我需要添加这个 header。还是要求?

Public Overrides Function GetResponse(response As IResponse, ByRef responseLength As Long, ByRef redirectUrl As String) As Stream

Dim aURI As New Uri(response)
Dim fileName As String = aURI.AbsolutePath

Dim bytes As Byte() = File.ReadAllBytes(fileName)
Dim mStream = New MemoryStream(bytes)

If mStream Is Nothing Then
    Return Nothing
Else
    Stream = mStream
    Stream.Position = 0
    responseLength = Stream.Length
    Dim fileExtension = Path.GetExtension(fileName)
    MimeType = GetMimeType(fileExtension)
    StatusCode = CInt(HttpStatusCode.OK)
    response.Headers.Add("Access-Control-Allow-Origin", "*")
    Return mStream
End If
End Function

任何评论此内容的人。 Amaitland 的评论纠正了我的问题。

网站的域名是https://product.company.de,我的方案是test。因此,一种方法如下。

CefSharp.Cef.AddCrossOriginWhitelistEntry("https://product.company.de", "test", "", True)

我做错的另一件事是我在应用程序启动时添加了它。但它返回错误。在我的示例 https://product.company.de 中,一旦浏览器实际位于网站上,您就需要添加它。在启动时将其与所有其他设置一起设置意味着它 returns 下降。我已将其放入 FrameLoadEnd 事件中。