Ahmed S. Durrani
Ahmed S. Durrani

Reputation: 1595

"That assembly does not allow partially trusted callers" on deployment

I have been trying to ExportPDF using the iTextSharp dll file. The code works fine locally but gives following error.

{Message: "That assembly does not allow partially trusted callers.",…}
ExceptionType
:
"System.Security.SecurityException"
Message
:
"That assembly does not allow partially trusted callers."
StackTrace
:
"   at Admin_WebMethods.ExportToPDF(String html, String IssuedToemailID)"

This is ExportPDF(..)

[WebMethod]
    public static int ExportToPDF(string html, string IssuedToemailID)
    {
        int status = 0;
        try
        {
            string FileName = "Invoice_" + DateTime.Now.ToString("M_dd_yyyy_H_M_s");

            html = html.Replace(">", ">");
            html = html.Replace("&lt;", "<");
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".pdf");

            StringReader sr = new StringReader(html);
            Document pdfDoc = new Document(PageSize.A4, 10f, 2f, 2f, 2f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            //PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
            string path = @"~\PdfGeneration\" + FileName + ".pdf";
            PdfWriter.GetInstance(pdfDoc, new FileStream(HttpContext.Current.Server.MapPath(path), FileMode.Create));
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            HttpContext.Current.ApplicationInstance.CompleteRequest();

            // send email here                     
            sendemail(path, IssuedToemailID);

            status = 1;
        }
        catch (Exception exp)
        {
            status = 0;
        }
        return status;

    }

This is how I am calling this method from JavaScript file

  function SaveToPDF() {
            $('div[id$=divStockoutPdf] > div[id$=divItems]').html($('#divStockoutItems').html());
    
            //remove the delete button from pdf
            $('div[id$=divStockoutPdf] > div[id$=divItems] table td:last').remove();
            $('div[id$=divStockoutPdf] > div[id$=divItems] table th:last').remove();
    
            var IssuedToemailID = $('#txtStockoutIssuedToEmail').val();
    
            var contents = $('#divStockoutPdf').html(); //$('#divStockoutItems').html();
            contents = contents.replace(/>/g, '&gt;');
            contents = contents.replace(/</g, '&lt;');
    
            $.ajax({
                type: "POST",
                url: "WebMethods.aspx/ExportToPDF",
                data: "{'html': '" + contents + "','IssuedToemailID': '" + IssuedToemailID + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (msg.d == 1) {
                        $('#btnCloseSignaure').click();
                        UpdateStockout();
                    }
                    else
                        alert("Internal Error. Kindly contact support team. Error at SaveToPDF");
                },
                error: function (er) { alert(er.error) }
            });
        }

I have tried all the solutions available online but nothing seems to be working or I might not been able to implement the solution properly. I have followed this

Upvotes: 0

Views: 1405

Answers (1)

rahulaga-msft
rahulaga-msft

Reputation: 4164

looks like you explicitly need to make ISharpText assembly to allow partially trusted users. Below link might help :

https://www.aspsnippets.com/Articles/ASPNet-iTextSharp-SystemSecuritySecurityException-That-assembly-does-not-allow-partially-trusted-callers.aspx

Upvotes: 2

Related Questions