title: .NET Framework 4.5 Security: SSRS calls assembly in C# generating error
body: <p>I have a SSRS report that references a dll assembly with one class that should call a stored procedure but returns this error. </p>
  
  <p><strong>Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, system.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.</strong></p>
  
  <p>I don't know C# but have been limping through this with the help of some friends and colleagues.  One had me add: </p>
  
  <pre><code>using System.Security.Permissions;
  SqlClientPermission oPerm = new SqlClientPermission(PermissionState.Unrestricted);
  oPerm.Assert();
  </code></pre>
  
  <p>to the class and I got this error:
  <strong>Cannot perform CAS Asserts in Security Transparent methods</strong></p>
  
  <p>Leading me to look at what changed after .NET 4.0.  I removed the SQLClientPermissions bit and added this to the AssemblyInfo:</p>
  
  <pre><code>[assembly: SecurityRules(SecurityRuleSet.Level2)]
  [assembly: SecurityCritical()]
  </code></pre>
  
  <p>Which returned the original error.  This is the Class:</p>
  
  <pre><code>using System.Text;
  using System.Threading.Tasks;
  using System.Data;
  using System.Data.SqlClient;
  using System.Security;
  using System.Security.Permissions;
  namespace ClassLibrary1
  {
      public class Class1
      {
          public static string logReportAttributes(string ReportName, int GlobalsTotalPages)
          {
              string retValue = String.Empty;
              try
              {
                  long TOCExecutionID = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmss"));
                  SqlConnection connection = new SqlConnection("Persist Security Info=True;Trusted_Connection=True;initial catalog=InternalApps;data source=DEVSQL1;User ID=xxxxx;password=xxxxx;MultipleActiveResultSets=true;");
                  using (SqlCommand cmd = new SqlCommand("usp_LogReportAttributes", connection))
                  {
                      cmd.CommandType = CommandType.StoredProcedure;
                      cmd.Parameters.AddWithValue("@ReportName", ReportName);
                      cmd.Parameters.AddWithValue("@GlobalsTotalPages", GlobalsTotalPages);
                      connection.Open();
                      cmd.ExecuteNonQuery();
                  }
                  if (connection.State == ConnectionState.Open || connection.State == ConnectionState.Broken)
                  {
                      connection.Close();
                  }
                  retValue = "Sent Successfully";
              }
              catch (Exception ex)
              {
                  retValue = string.Format ( "{0} \n\r {1}" , ex.Message != null ? ex.Message : "" , ex.InnerException != null ? ex.InnerException.Message : "");
              }
               return retValue;
          }
      }
  }
  </code></pre>
  
  <p>Does anyone have any idea what I'm doing wrong and how to get the correct security set up for this?  Thank you!</p>