【问题标题】:"That assembly does not allow partially trusted callers" on deployment部署时“该程序集不允许部分受信任的调用者”
【发布时间】:2018-02-05 04:01:30
【问题描述】:

我一直在尝试ExportPDF 使用 iTextSharp dll 文件。该代码在本地运行良好,但出现以下错误。

{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)"

这是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;

    }

这就是我从 JavaScript 文件调用此方法的方式

  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) }
            });
        }

我已经尝试了所有在线可用的解决方案,但似乎没有任何效果,或者我可能无法正确实施该解决方案。我关注了this

【问题讨论】:

  • 与您的问题无关,但您不应使用HTMLWorker,因为它已被弃用。请改用 iText 7 + pdfHTML。

标签: javascript c# .net itext


【解决方案1】:

看起来您明确需要制作 ISharpText 程序集以允许部分受信任的用户。下面的链接可能会有所帮助:

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

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多