ssrs函数返回图像的system.security.permissions.securitypermission错误



ssrs在VS2015中使用自定义函数使用zxing.dll将值转换为条形码图像。我发现本文使用Visual Studio Local Report或SSR生成条形码,该报告与我要做的事情和遵循它基本相同。当我预览报告时,它会产生下面的警告,图像显示了红色X。我可以部署报告,但在相关列中没有显示图像。图像是使用正确的MIME类型(Image/BMP(设置的,但我尝试了其他结果相同的结果。

注意:我已经搜索并阅读了几篇文章,这些文章帮助我取得了进步,但似乎无法解决这个问题。

完整的错误是:

[rsRuntimeErrorInExpression] The Value expression for the image ‘ImageFromFunction’ contains an error: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
[rsInvalidDatabaseImageProperty] The value of the ImageData property for the image ‘ImageFromFunction’ is “=Code.TextToBarcode128Bmp(Fields!barcode_value.Value)”, which is not a valid ImageData.

我有一个WPF,其中相同的代码(在C#中(正常工作。在SSR中使用它不喜欢它。第一个消息似乎表明这是一种权限/版本的东西。我知道SSRS为.NET 2.0。我将引用引用到System.draws.2.0版本,并在2.0版本的zxing.dll上复制到服务器。

看起来它正在尝试获得安全权限(可能是zxing.dll?(,但是无法到达system.security.permissions.securityperrise?

参考

zxing, Version=0.15.0.0, Culture=neutral, PublicKeyToken=4e88037ac681fe60
System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

我尝试了对2.0版本的System.Security添加了引用。

这是自定义功能:

Public Function TextToBarcode(ByVal text As String) As Byte()
Dim writer As New ZXing.BarcodeWriter()
writer.Format = ZXing.BarcodeFormat.CODE_128
writer.Options = New ZXing.Common.EncodingOptions
writer.Options.Width = 125
writer.Options.Height = 100
Dim bmp As System.Drawing.Bitmap = writer.Write(text)
Dim ms AS System.IO.MemoryStream = new System.IO.MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim imagedata as byte()
imagedata = ms.GetBuffer()
Return imagedata
End Function

update

我遵循了文章SSRS:System.Security.permissions.securitypermission(感谢Michael和Superman(的步骤,该步骤基于本文如何使用自定义组件或报告服务中的嵌入式代码

在服务器上,我从D: Program Files Microsoft SQL Server MSRS12.MSSSQLSERVER Reporting Services Reportserver bin

中添加了zxing.dll。

在服务器上,我在D: Program Files Microsoft SQL Server MSRS12.MSSQLSERVER Reporting Services Reporterver rssrvpolicy.config:

<CodeGroup
    class="UnionCodeGroup"
    version="1"
    PermissionSetName="FullTrust"
    Name="ZXingCodeGroup"
    Description="Code group for ZXing">
    <IMembershipCondition
        class="UrlMembershipCondition"
        version="1"
        Url="D:Program FilesMicrosoft SQL ServerMSRS12.MSSQLSERVERReporting ServicesReportServerbinzxing.dll"
    />
</CodeGroup>

我重新启动了报告服务。行为没有变化:运行部署的报告没有产生错误,但不显示条形码。

在VS DEV系统上,我将CodeGroup添加到C: Program Files(X86( Microsoft Visual Studio 14.0 common7 IDE privateassemblies rspReviewPolicy.config.config和封闭和重新开放的Visual Studio。行为没有变化:报告的预览显示相同的警告,并且无法显示条形码。

update

我已经确认这是某种权限问题。通过评论执行zxing.barcoderwriter.write((调用的行不再产生此错误。实际上,通过用来自图像数据的字节数组的代码替换功能主体(下图(,它成功显示了图像:

Public Function getBarCodeHorizontal(ByVal text1 As String) As Byte()
Dim text as String = "89504E470D0A1A0A0000000D494844520000009600000028080200000093A394B0000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000026549444154785EED9AB16EDB400C864F7D112BC892C10F5103196474CD1B548F20C32FE1B1A3FC065D8B6A28203F84072F46EC2751792475C79364C0E822B1E0372824EF3FDAB93FC7C041B2AEEBDC145996C1135621204D0888C934EC0A155CE46E04C9C293AB424F6229A3546A085A22821EE24701E9A942C84AD00C0461952AC0584FABB2EE758894510ACF81009EA1422910768DB748BEF057432D66A17ACC42F59885EA310BD56316AAC72C548F59A81EB3503D66A17ACC42F59885EA310BD56316AAC72C548F59A81EB3503D66A17ACC42F59885EA310BD56316AAC72C544FF21F8F8646EC16AAC72C54CFFF61E169976D8F774EFE9DFB719BED4E9CA8C16EA17ACC42F52CCA4298878C188B7EBA31E9B00CEAEDF1CA254FD0F3484C87ACC8E2AB0D1AA7133599AEC3E64B603916C2E96CCEF50D3EE4745DBB2E733C2428E6E5BAC5E2AD765C05E0FC378EEADDFE521EA8EAEBACBFD5E70D8ABF7EAB9A9F7FD8A2D3AF43F1F1BEF2D16E73A8A8415B35E58F672C19375F04F84D2C805B5D383ED2C8A018D3B67205DB0DF83AA5A21EC5107131DDD61394E32089279BCFCF626EE1E7A529DE5E3879C0EA75EDCE57B850F7EB994B88AF079A32C75197970D89DDEAFDC3E13D8C7710E9A7A2573EC9B8F9FC2CC6C297B7A2B97C72F200EFDCFA152C483C1B20AFD9EFEFE8177B080E567BAAA07DF9658F2A7FA59E64A2F9FCF0FB991F7F90724E618CA7DB4F2C186449DCAB7DDC275123D5BE0F20669F588DDBFDAB0D37CB77F0A0F9CC2CC742000F9388E7836748F49E11415DD4B5F81D27CA429DFC2C7862DBAAED17FD57D684656C1E364E379F17FB33B77AECA3BD7ACC42E538F7178726B2F89D7D5EF80000000049454E44AE426082"
Dim bytes As Byte() = New Byte(text.Length  2 - 1) {}
Dim b as Byte
For i As Integer = 0 To text.Length - 1 Step 2
    bytes(i  2) = Byte.Parse(text(i).ToString() & text(i + 1).ToString(), System.Globalization.NumberStyles.HexNumber)
Next
Return bytes
End Function

update

我尝试使用zxing.dll公共密钥令牌将服务器上的代码组更改为强,但没有改进:

<CodeGroup
    class="UnionCodeGroup"
    version="1"
    PermissionSetName="FullTrust"
    Name="ZXingCodeGroup"
    Description="Code group for ZXing">
    <IMembershipCondition
        class="StrongNameMembershipCondition"
        version="1"
        PublicKeyBlob="4e88037ac681fe60"
    />
</CodeGroup>

哦,这是一个很好的..

进行了很多挖掘和调试(如果您可以调试调试 - 我将返回语句放入ZXing代码中,并观看何时会引起该错误(

我的设置中的一件事与您的设置不同:我使用自定义DLL与zxing.dll交互。

它归结为他们将Pixel-Data放入位图的方式:
https://github.com/micjahn/zxing.net/blob/eef88ee53ced1035eade39f4454444444444444444444444444444444444444444444444444444444444444444444444.D2DB6B6B6B6B6/Source/lib/renderer/renderer/bitmaprenderer.cs#l169

解决方法是手动转换:

public static string Test()
    // Don't let ZXing handle the conversion to a bitmap -> give me raw pixel data
    var writer = new BarcodeWriterPixelData { Format = BarcodeFormat.DATA_MATRIX };
    var result = writer.Write("Test123");
    Bitmap bmp = new Bitmap(result.Width, result.Height);
    int bpp = 32;
    for (int y = 0; y < result.Height; y++)
    {
        for (int x = 0; x < result.Width; x++)
        {
            int i = ((y * result.Width) + x) * (bpp / 8);
            if (bpp == 32) // in this case you have 4 color values (red, green, blue,alpha)
            {
                // first byte will be red, because you are writing it as first value
                byte r = result.Pixels[i];
                byte g = result.Pixels[i + 1];
                byte b = result.Pixels[i + 2];
                byte a = result.Pixels[i + 3]; // Alpha?
                Color color = Color.FromArgb(r, g, b);
                bmp.SetPixel(x, y, color);
            }
        }
    }
    return GetBase64(bmp);
}
public static string GetBase64(Image image)
{
    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, ImageFormat.Png);
        byte[] imageBytes = m.ToArray();
        return Convert.ToBase64String(imageBytes);
    }
}

源(稍作修改(:https://stackoverflow.com/a/16130425/11829240

还要注意,您可能需要自己构建它才能将[assembly:AllowPartiallyTrustedCallers]插入AssemblyInfo.cs

完美的解决方案是生成SVG,但SSR不喜欢。

更多地阅读该Bitmap.LockBits主题:
https://www.codeproject.com/articles/625868/lockbits-anternative-securityexception-workaround

还找到了以下内容:

嗨,

我正在尝试在CRM 2011(在房屋(报告中创建条形码。不幸的是,由于沙箱和其他各种问题,这是不可能的。我尝试了几种方法,但没有任何作用。

因此,我现在的方法是创建一个C#插件,该插件在创建记录时生成条形码图像,然后将图像字节存储为字符串,然后在报告中使用Convert.Frombase64String来显示条形码映像。然后,这种方法应该有效...

但是,经过几天的尝试,我遇到了许可安全的问题

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

,并已调试到渲染中不起作用/兼容的部分(对于CRM 2011 ..!(

var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

所以我的问题是:有没有锁定的方法?
这里的任何帮助将得到很大的收获。

不幸的是,链接已经死了:http://zxingnet.codeplex.com/discussions/584679

编辑:实际上挖了它,存档的存档..
https://web.archive.org/web/20210303162237/https://archive.codeplex.com/?p=zxingNet
单击讨论,然后搜索Alternative to LockBits due to Security issues

codeplex-archive-download(zip(:
https://web.archive.org/web/20210701004220/https://codeplexarchive.blob.core.core.windows.net/archive/projecn/project/project/project/zxingnet/zxingnet/zxingnet.zip.zip.zip

相关内容

最新更新