0

我最近尝试在 C# .NET Core 3.1 Web 应用程序中解密和验证 PKCS#7 消息。这是我的做法

System.Security.Cryptography.Pkcs在 Web 应用程序项目中使用过。但是在实现相同的包并尝试在Azure 函数中解密和验证后,它返回异常

{"Could not load file or assembly 'System.Security.Cryptography.Pkcs, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.":"System.Security.Cryptography.Pkcs, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}.

我尝试了几个堆栈溢出答案,但无法对其进行排序。

System.Security.Cryptography.X509Certificates在 Azure 功能中运行良好,但Pkcs似乎有问题。

反正我能解决吗?

这是我的 .csproj:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<UserSecretsId>695a883e-9b1a-4d55-98a9-0b47b683bbb2</UserSecretsId>
<Nullable>annotations</Nullable>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Autofac" Version="6.1.0" />
    <PackageReference Include="Dapper" Version="2.0.78" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.4.2" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
    <PackageReference Include="System.Collections" Version="4.3.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
    <PackageReference Include="System.Security.Cryptography.Pkcs" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
4

1 回答 1

1

Mishra]( https://stackoverflow.com/users/13378259/rocky-mishra ) 很高兴您的问题得到解决,并将您的建议作为答案发布,以帮助面临类似问题的其他社区成员。

下面的示例代码有助于解决问题。

public class FunctionsAssemblyResolver
{
    public static void RedirectAssembly()
    {
        var list = AppDomain.CurrentDomain.GetAssemblies().OrderByDescending(a => a.FullName).Select(a => a.FullName).ToList();
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }

    private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        var requestedAssembly = new AssemblyName(args.Name);
        Assembly assembly = null;
        AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
        try
        {
            assembly = Assembly.Load(requestedAssembly.Name);
        }
        catch (Exception ex)
        {
        }
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        return assembly;
    }

}

有关更多信息,请查看此SO

于 2021-12-08T08:26:19.977 回答