Blazor WebAssembly 应用程序由于完整性错误而无法加载

Sim*_*onS 9 browser security blazor

我们开发了 Blazor WebAssembly 应用程序,该应用程序已为特定客户群投入生产使用。

该应用程序在所有具有标准安全设置的浏览器中运行良好。然而,今天早上我接到一位客户的电话,他们的 Chrome 浏览器中根本没有加载该应用程序。

我在控制台中看到以下错误:

  • Unknown error occurred while trying to verify integrity.
  • Failed to load resource: the server responded with a status of 403 (forbidden)
  • Failed to find a valid digest in the 'integrity' attribute for ressource '<somepath.dll>' with SHA-256 integrity <sha56>. the resource has been blocked

现在我的问题是,什么可能导致这种情况?这是浏览器安全设置,还是其他安全设置,例如服务器上、代码中的设置等?我怎样才能解决这个问题?

这是上面提到的错误的图片

Blazor Chrome 控制台错误

小智 6

我的解决方案是删除客户端和服务器项目文件夹中的obj和文件夹bin


Sim*_*onS 5

2023 年 8 月 30 日更新:

我们发现 Blazor 应用程序无法工作的网络使用转发代理或防火墙来阻止 .dll 和 .bin 文件所属的 MIME 类型“application/octet-stream”。如果您的管理员将其添加到您的环境中,您基本上可以测试此错误。目前,我们正在尝试将 Blazor 文件打包为非八位字节流,或者将其作为带有 .net 8 预览版的 wasm 文件(MIME 类型“application/wasm”)发送。

2023 年 7 月 5 日更新:

看起来随着 .NET 8.0 的发布,Blazor 创建 .wasm 文件而不是 .dll。希望这将消除 Blazor WebAssembly 应用程序的防火墙 + 防病毒块。引用:

在 ASP.NET Core 8.0 或更高版本中,.NET 程序集使用 Webcil 文件格式部署为 WebAssembly 文件 (.wasm)。

2023 年 7 月 5 日之前答复:

发生这种情况的最可能原因是某些防病毒软件阻止下载.dll文件的执行。这就是为什么它在某些网络中有效,但在其他网络中无效。

您可以做的,以及microsoft 也建议的解决方法,是将所有内容重命名.dll.bin- 并更改配置 json。它对我来说有效。

我为此使用以下 PowerShell 函数:

Function Hide-BlazorDLL {

    Param(
        [string]$Path = (Get-Location).Path
    )

    <#
        According to the following Links:
        https://github.com/dotnet/aspnetcore/issues/19552
        https://github.com/dotnet/aspnetcore/issues/5477#issuecomment-599148931
        https://github.com/dotnet/aspnetcore/issues/21489
        https://gist.github.com/Swimburger/774ca2b63bad4a16eb2fa23b47297e71 
    #>

    # Test if path is correct and accessible
    $WorkingDir = Join-Path $Path "_framework"
    if (!(Test-Path $WorkingDir)) { Throw "Wrong path $Path. current location must be wwwroot folder of published application." }

    # Get All Items
    $AllItems = Get-ChildItem $WorkingDir -Recurse
    $DLLs = $AllItems | Where-Object { $_.Name -like '*.dll*' }
    $BINs = $AllItems | Where-Object { $_.Name -like '*.bin*' }

    # End script if no .dll are found
    if ($DLLs) { 

        # Delete all current .bin files
        if ($BINs) {
            Remove-item $BINs.FullName -Force
        }
    
        # Change .dll to .bin on files and config
        $DLLs | Rename-item -NewName { $_.Name -replace ".dll\b",".bin" }
        ((Get-Content "$WorkingDir\blazor.boot.json" -Raw) -replace '.dll"','.bin"') | Set-Content "$WorkingDir\blazor.boot.json"
    
        # Delete Compressed Blazor files
        if (Test-Path "$WorkingDir\blazor.boot.json.gz") {
            Remove-Item "$WorkingDir\blazor.boot.json.gz"
        }
        if (Test-Path "$WorkingDir\blazor.boot.json.br") {
            Remove-Item "$WorkingDir\blazor.boot.json.br"
        }
    
        # Do the same for ServiceWorker, if it exists
        $ServiceWorker = Get-Item "$Path\service-worker-assets.js" -ErrorAction SilentlyContinue
        if ($ServiceWorker) {
            ((Get-Content $ServiceWorker.FullName -Raw) -replace '.dll"','.bin"') | Set-Content $ServiceWorker.FullName
            Remove-Item ($ServiceWorker.FullName + ".gz")
            Remove-Item ($ServiceWorker.FullName + ".br")
        }
    }
    else {
        Write-Host "There are no .dll Files to rename to .bin" 
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,您需要导航到wwwroot已发布应用程序的文件夹并在那里运行该函数。例如:

PS D:\inetpub\wwwroot\<appname>\wwwroot> Hide-BlazorDLL
Run Code Online (Sandbox Code Playgroud)