报表查看器 - 请求 SqlClientPermission 类型的权限失败
我在本地模式下使用 Visual Studio 2008 中的 ReportViewer 控件,并将对象作为数据源。 我的类映射到数据库中的数据表。 在对象中,它根据需要加载相关对象。 因此,它会将引用保留为空,直到您尝试使用该属性,然后它会尝试自动从数据库加载它。 这些类使用 System.Data.SqlClient 命名空间。
当我与 Windows 窗体应用程序中的对象交互时,一切都按预期进行。 但是,当我传递要用作报表数据源的对象并尝试自动加载相关对象时,它失败了。 该代码创建一个 SqlConnection 对象,当我对其调用 GetCommand() 时,会引发以下异常:
[System.Security.SecurityException] {
"Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
} System.Security.SecurityException
我已尝试搜索错误,但显示的所有结果都是针对在 SQL Server 或 ASP 上运行的 CLR 程序集。网。 在创建 SqlConnection 对象之前,我尝试在代码中添加以下调用(如搜索结果中的建议),但它显然没有执行任何操作:
System.Data.SqlClient.SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted).Assert();
有什么想法吗?
I am using the ReportViewer control from Visual Studio 2008 in Local Mode with objects as the data source. My classes are mapped to data tables in my database. In the objects, it loads related objects as needed. So it leaves the reference null until you try to use the property, then it tries to load it from the database automatically. The classes use the System.Data.SqlClient namespace.
When I interact with the objects in my Windows Forms application, everything works as expected. But when I pass the object to be used as a Report Data Source and it tries to automatically load the related object, it fails. The code creates a SqlConnection object and when I call GetCommand() on it, the following exception is thrown:
[System.Security.SecurityException] {
"Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
} System.Security.SecurityException
I've tried searching for the error, but all the results that show up are for CLR assemblies running on a SQL Server or ASP.Net. I've tried adding the following call in my code (as suggested in the search results) before creating the SqlConnection objects, but it didn't apparently do anything:
System.Data.SqlClient.SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted).Assert();
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除了CuppM的回答。
自 .NET4 起,
ExecuteReportInCurrentAppDomain
方法已被弃用,应改用LocalReport.SetBasePermissionsForSandboxAppDomain
,因为 ReportViewer 现在始终在沙盒域中执行:请参阅详细信息此处。
In addition to the answer of CuppM.
The
ExecuteReportInCurrentAppDomain
method is deprecated since .NET4, andLocalReport.SetBasePermissionsForSandboxAppDomain
should be used instead, as ReportViewer is now always executed in sandboxed domain:See details here.
我找到了解决方案。 您可以将执行程序集(或具有足够权限的程序集)的 System.Security.Policy.Evidence 指定给 LocalReport,以便在执行期间使用。
I've found the solution. You specify System.Security.Policy.Evidence of you executing assembly (or one that has sufficient rights) to the LocalReport for use during execution.
以防万一有人像我一样在搜索此权限错误时偶然发现这一点。
我在使用 Windows-Forms-Application 时遇到此错误,因为客户使用“\COMPUTERNAME\C$\Application.exe”而不是“C:”在他的计算机上链接了指向我的 Application-Exe 的快捷方式。 \应用程序.exe。” - 由于不受信任的 Intranet 使用,导致 System.Security.Permission 失败。
请参阅http://www.duelec.de/blog/?p=236了解更多信息。
Just in case someone stumbles upon this like I did while searching for this Permission-Error.
I got this error using a Windows-Forms-Application because the customer had linked a shortcut to my Application-Exe on his machine with "\COMPUTERNAME\C$\Application.exe" instead of "C:\Application.exe." - This caused the failure of the System.Security.Permission because of the untrusted intranet use.
See http://www.duelec.de/blog/?p=236 for more Information.
上面 Artem 答案的脚注...
我在将 Windows 身份验证添加到我的 asp.net 应用程序时遇到了这个问题。 以 Framework 4.5 为目标并使用报告组件 11. 当我允许匿名用户(在早期开发中)时,我使用 ReportViewer 没有任何问题。 一旦启用 Windows 身份验证,我要么在分组表达式上收到“#Error”,要么根本无法运行报告,出现上面列出的异常。
我能够解决这个问题,但对 Artem 发布的内容进行了稍微修改。 我不完全确定该代码的作用,除了一般意义上它允许 CAS 信任沙盒 ReportViewer 代码。 任何带有一点解释的评论将不胜感激。
A footnote to Artem's answer above...
I had this problem when adding Windows Authentication to my asp.net app. Targeting Framework 4.5 and using Reporting components 11. When I was allowing anonymous users (in early dev) I had no problems using the ReportViewer. As soon as I enabled Windows auth I would either get "#Error" on Grouping expressions, or not be able to run the report at all, giving the exception listed above.
I was able to work around the problem but with a slightly modified version of what Artem posted. I am not completely sure what the code does other than a general sense that it allows CAS to trust the sandboxed ReportViewer code. Any comments with a little explanation would be appreciated.
一个快速的想法,虽然这不是我见过的错误,但请确保您的断言与设置资源数据源的代码采用相同的方法:
权限断言不会长时间停留,它们可以这是一个安全问题,因此尽可能靠近需要它们的代码最有可能起作用。
One quick thought, although this isn't an error I've seen, make sure that your Assert is in the same method as the code that is setting the resource data source:
Permission Asserts don't hang around for long, they can be a security issue, so doing them as near as possible to the code that needs them is most likely to work.