Report Viewer - request permission permission of type SqlClientPermission

I am using a ReportViewer control from Visual Studio 2008 in local mode with objects as a data source. My classes are mapped to data tables in my database. In objects, it loads related objects as needed. Therefore, it leaves reference zero until you try to use this property, and then try to load it from the database automatically. Classes use the System.Data.SqlClient namespace.

When I interact with objects in a Windows Forms application, everything works as expected. But when I pass in an object that will be used as the report data source, and it tries to automatically load the associated object, it fails. The code creates an 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 tried to find the error, but all the results that appear are intended to build CLRs running on SQL Server or ASP.Net. I tried adding the following code to my code (as suggested in the search results) before creating SqlConnection objects, but apparently did nothing:

 System.Data.SqlClient.SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted).Assert(); 

Any ideas?

+6
c # visual-studio-2008 winforms reporting-services sqlclient
source share
5 answers

I have found a solution. You specify System.Security.Policy.Evidence that you are assembling (or one that has sufficient rights) for LocalReport to use at runtime.

 reportViewer.LocalReport.ExecuteReportInCurrentAppDomain(System.Reflection.Assembly.GetExecutingAssembly().Evidence); 
+4
source share

In addition to the CuppM answer. The ExecuteReportInCurrentAppDomain method has ExecuteReportInCurrentAppDomain deprecated since .NET4 and LocalReport.SetBasePermissionsForSandboxAppDomain should be used instead, since ReportViewer is now always executed in an isolated domain:

 PermissionSet permissions = new PermissionSet(PermissionState.None); permissions.AddPermission(new FileIOPermission(PermissionState.Unrestricted)); permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); ReportViewer1.LocalReport.SetBasePermissionsForSandboxAppDomain(permissions); 

More details here .

+7
source share

Just in case, someone stumbles upon this, as during the search for this permission error. I got this error using Windows-Forms-Application because the client associated the shortcut with my Application-Exe on their machine using "\ COMPUTERNAME \ C $ \ Application.exe" instead of "C: \ application.exe". - This caused a System.Security.Permission failure due to unreliable use within the network.

See http://www.duelec.de/blog/?p=236 for more details.

+1
source share

Note to Artyom above ...

I am having a problem adding windows authentication to my asp.net application. Targeting Framework 4.5 and using reporting components 11. When I allowed anonymous users (in the early dev), I had no problems using ReportViewer. As soon as I turned on Windows auth, I would either get a "#Error" when grouping expressions, or I couldn’t run the report at all, indicating the exception mentioned above.

I was able to get around the problem, but with a slightly modified version of what Artem posted. I'm not quite sure what the code does, other than the general one, that it allows CAS to trust the isolated ReportViewer program code. Any comments with a little explanation will be appreciated.

  Dim permissions As PermissionSet = New PermissionSet(PermissionState.Unrestricted) myReportViewer.LocalReport.SetBasePermissionsForSandboxAppDomain(permissions) 
+1
source share

A quick thought, although this is not an error that I saw, make sure your Assert is in the same method as the code setting the resource data source:

 System.Data.SqlClient.SqlClientPermission mPermission = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted); try { mPermission.Assert(); //rest of your code } //Handle Exceptions 

Removing permissions does not last long, they can be a security problem, so they are as close as possible to the code that they need, most likely, will work.

0
source share

All Articles