简体   繁体   English

SSRS 自定义程序集权限问题

[英]SSRS Custom Assembly Permissions issue

I am trying to create a custom SSRS assembly that formats the style of a report based on the value of an SQL table.我正在尝试创建一个自定义 SSRS 程序集,该程序集根据 SQL 表的值格式化报告的样式。

I need to be able to preview this locally and deploy to the reporting server.我需要能够在本地预览并部署到报告服务器。

This would allow me to roll out style changes to a series of Paginated Reports from a central place.这将允许我从一个中心位置对一系列分页报告进行样式更改。

Example expression: =ProjectName.ClassName.getItemStyle("Header", "Font-Family") The expression parses the values I'm looking for then goes off and returns a value from the SQL table, in this case "Verdana".示例表达式: =ProjectName.ClassName.getItemStyle("Header", "Font-Family")该表达式解析我要查找的值,然后从 SQL 表中返回一个值,在本例中为“Verdana”。

I've been through various steps to get this working as far as I have and have now reached a permissions problem.我已经通过各种步骤来使其正常工作,现在已经遇到权限问题。 The assembly works fine so long as I don't try to connect to an SQL server.只要我不尝试连接到 SQL 服务器,该组件就可以正常工作。

In which case I receive this error:在这种情况下,我收到此错误:

The Value expression for the textrun 'Textbox1.Paragraphs[0].TextRuns[0]' contains an error: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. textrun 'Textbox1.Paragraphs[0].TextRuns[0]' 的值表达式包含错误:请求类型'System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=的权限中立,PublicKeyToken=b77a5c561934e089' 失败。

I have tried adding in assembly permissions and editing the rssrvpolicy config file to no avail, but I feel like I am missing something and I'm not too sure I understand the Microsoft documentation on this.我尝试添加程序集权限并编辑 rssrvpolicy 配置文件无济于事,但我觉得我遗漏了一些东西,我不太确定我是否了解 Microsoft 文档。

I have also signed the assembly and installed the certificate.我还签署了程序集并安装了证书。

Code I've used:我用过的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Security;
[assembly: AllowPartiallyTrustedCallers]

namespace CC_rpt
{   
    public class lib
    {
        public static string getItemStyle(string parse)
        {
            string val;
            val = "1";

            try
            {              
                using (SqlConnection oConn = new SqlConnection("Data Source=DBServer;Initial Catalog=DBName;Integrated Security=SSPI"))
                {
                    var oCmd = new System.Data.SqlClient.SqlCommand();
                    oCmd.Connection = oConn;
                    oCmd.CommandText = "select singleValue FROM Table WHERE Column = @Param";
                    oCmd.Parameters.AddWithValue("@Param", parse);
                    val = oCmd.ExecuteScalar().ToString();
                }
            }

            catch (System.Data.SqlClient.SqlException e)
            {
                val = e.ToString();
            }

            return val;
        }
    }
}

OS : Windows 7 Enterprise Version 6.1 (Build 7601: Service Pack 1)操作系统:Windows 7 企业版 6.1(内部版本 7601:Service Pack 1)

Visual Studio : Microsoft Visual Studio Professional 2017 Version 15.9.13 Visual Studio :Microsoft Visual Studio Professional 2017 版本 15.9.13

Server SSRS Reporting Services 2016服务器SSRS 报告服务 2016

Installed Products :已安装产品
SQL Server Reporting Services 15.0.1528.0 SQL Server Integration Services 15.0.1300.371 SQL 服务器报告服务 15.0.1528.0 SQL 服务器集成服务 15.0.1300.371

.Net libraries I'm primarily trying to use: System.Data.SqlClient targeting.Net Framework: 4.6.1我主要尝试使用的.Net 库: System.Data.SqlClient目标.Net Framework:4.6.1

Paths I'm copying the DLL to (Via a post build event)*:我将 DLL 复制到的路径(通过构建后事件)*:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\MSBuild C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\PrivateAssemblies C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Reporting Services C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\SSRS C:\Program Files\Microsoft SQL Server\MSRS11.MSSQLSERVER\Reporting Services\ReportServer\bin C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\MSBuild C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\PrivateAssemblies C:\Program文件 (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Reporting Services C:\Program Files 2017\Professional\Common7\IDE\CommonExtensions\Microsoft\SSRS C:\Program Files\Microsoft SQL 服务器\MSRS11.MSSQLSERVER\Reporting Services\ReportServer\bin

Assembly security settings: I have signed the assembly created the string key file (pfx) and installed it.程序集安全设置:我已经签署了程序集,创建了字符串密钥文件 (pfx) 并安装了它。

If you are having issue with the Visual Studio preview, then the solution from @bergerb is correct but you are looking at different folder/file如果您在使用 Visual Studio 预览时遇到问题,那么来自 @bergerb 的解决方案是正确的,但您正在查看不同的文件夹/文件

  1. The folder should be something like C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\SSRS该文件夹应类似于 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\SSRS

  2. The file you need to edit is RSPreviewPolicy.config in the same location您需要编辑的文件是同一位置的 RSPreviewPolicy.config

  3. Add this code from @bergerb从@bergerb 添加此代码

<CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust" Name="DBAccessPermissionCodeGroup" Description="A special code group for my custom assembly.">
    <IMembershipCondition class="UrlMembershipCondition" version="1" Url="c:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\bin\Your_Custom_DLL_Name.dll" />
</CodeGroup>

Okay.好的。 Same problem, 30 Firefox Tabs later:同样的问题,30 Firefox 选项卡稍后:

My jurney starts here: http://bryantlikes.com/WritingCustomCodeInSQLServerReportingServices.aspx我的旅程从这里开始: http://bryantlikes.com/WritingCustomCodeInSQLServerReportingServices.aspx

You need to add a CodeGroup to this file: c:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\rssrvpolicy.config您需要在此文件中添加 CodeGroup: c:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\rssrvpolicy.config

Couldn't really get it to work with a Custom Permission Set, so I used FullTrust.无法真正让它与自定义权限集一起使用,所以我使用了 FullTrust。 Maybe not the cleanest?也许不是最干净的?

My last version of the CodeGroup looks as follows:我的 CodeGroup 的最新版本如下所示:

<CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust" Name="DBAccessPermissionCodeGroup" Description="A special code group for my custom assembly.">
    <IMembershipCondition class="UrlMembershipCondition" version="1" Url="c:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\bin\Your_Custom_DLL_Name.dll" />
</CodeGroup>

By the way: Check the url of the dll, might seem obvious but yeah, don't ask me how I know..顺便说一句:检查 dll 的 url,可能看起来很明显,但是,别问我怎么知道的。

Then you gotta mark your Methods (and/or maybe Classes?) as [SecuritySafeCritical] , because SSRS's Expression thingy runs as SecurityTransparent.然后你必须将你的方法(和/或类?)标记为[SecuritySafeCritical] ,因为 SSRS 的 Expression thingy 作为 SecurityTransparent 运行。 ( https://stackoverflow.com/a/34850214/11829240 ) https://stackoverflow.com/a/34850214/11829240

If you don't do that, you will get an error that says something like Cannot perform CAS Asserts in Security Transparent methods如果你不这样做,你会得到一个错误,上面写着Cannot perform CAS Asserts in Security Transparent methods

And you also want to add the following Attribute to the Method that uses the SQL Stuff [SqlClientPermission(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]而且您还想将以下属性添加到使用 SQL 东西的方法[SqlClientPermission(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]

If you leave this out, you will get exactly the error you are experiencing.如果你忽略它,你将得到你所遇到的错误。

And in my AssemblyInfo.cs I added [assembly:AllowPartiallyTrustedCallers] ( https://stackoverflow.com/a/19214614/11829240 )在我的 AssemblyInfo.cs 中,我添加了[assembly:AllowPartiallyTrustedCallers] ( https://stackoverflow.com/a/19214614/11829240 )

I don't know if it is necessary, but my.dll is signed.不知道有没有必要,但是my.dll是签名的。

When finished, I enabled TCP/IP in my SQL Server and created a new user (and enabled SQL-Server authentication).完成后,我在 SQL 服务器中启用了 TCP/IP 并创建了一个新用户(并启用了 SQL-Server 身份验证)。

By the way: to debug #Error messages, I used prodcump.顺便说一句:为了调试#Error 消息,我使用了 prodcump。 ./procdump.exe -f "" -l -e 1 ReportingServicesService.exe

https://docs.microsoft.com/en-us/sysinternals/downloads/procdump https://docs.microsoft.com/en-us/sysinternals/downloads/procdump

I hope this helps someone, because that was not funny to research.我希望这对某人有所帮助,因为研究起来并不有趣。 But now my custom assembly finally works.但是现在我的自定义程序集终于可以工作了。

My connection string: Data Source=localhost,1433;Initial Catalog=database;User Id=user;Password=pass;我的连接字符串: Data Source=localhost,1433;Initial Catalog=database;User Id=user;Password=pass;

This PermissionSet did NOT work for me:这个 PermissionSet 对我不起作用:

<PermissionSet class="NamedPermissionSet" version="1" Name="DBAccessPermissionSet" Description="A special permission set that grants access to Databases.">
  <IPermission class="SqlClientPermission" version="1" Unrestricted="true" />
  <IPermission class="SecurityPermission" version="1" Flags="Execution, Assertion" />
</PermissionSet>

It somehow also throws the Cannot perform CAS Asserts in Security Transparent methods Error.它还以某种方式引发Cannot perform CAS Asserts in Security Transparent methods错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM