2

我正在使用 ASPNETZERO 的 Angular 4 + .Net Core。

我有一个显示用户提交表单列表的网格和一个带有打印表单的按钮的列。

这是我的打印功能;我在输入中传递了 ConvertUrl() 方法的 url:

    print(item: FormSummaryDto) {
        this.beginTask();

        let formUrl = AppConsts.appBaseUrl + '/#/app/main/form/' + item.formType.toLowerCase() + '/' + item.id + '/print';

        let input = new ExportFormInput({ formId: item.id, formUrl: formUrl, includeAttachments: true });

        this.service.exportFormToPdf(input)
            .finally(() => { this.endTask(); })
            .subscribe((result) => {
                if (result == null || result.fileName === '') {
                    return;
                }

                this._fileDownloadService.downloadTempFile(result);
            }, error => console.log('downloadFile', 'Could not download file.'));
    }

在转换和下载文件的过程中一切正常,但是,当我进行转换(如下)时,由于身份验证,url 重定向到登录页面,并且该页面正在被转换。

HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertUrl(url);
doc.Save(file);
doc.Close();

我不知道如何将 SelectPdf 的身份验证选项与 ASPNETZERO 一起使用,并希望有人知道我可以传递当前会话/凭据的方法,或者如何使用 SelectPdf 的身份验证选项之一,以便转换传递的 url。

感谢!

工作组

4

2 回答 2

2

在身份验证 cookie 的 SelectPdf 文档中让我失望的是示例中的 System.Web.Security.FormsAuthentication.FormsCookieName,我认为它应该是这样的。

// set authentication cookie
converter.Options.HttpCookies.Add(
    System.Web.Security.FormsAuthentication.FormsCookieName,
     Request.Cookies[FormsAuthentication.FormsCookieName].Value);

但我得到以下异常:

System.TypeLoadException: Could not load type 'System.Web.Security.FormsAuthentication' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

我最终意识到我需要传递 ASPNETZERO 身份验证 cookie(在查看 cookie 文件夹后是“Abp.AuthToken”)。我没有尝试在服务方法中获取 cookie 值,而是在调用参数中传递了它:

    print(item: FormSummaryDto) {
        this.beginTask();

        let formUrl = AppConsts.appBaseUrl + '/#/app/main/form/' + item.formType.toLowerCase() + '/' + item.id + '/print';
        let authToken = abp.utils.getCookieValue('Abp.AuthToken');

        let input = new ExportFormInput({ formId: item.id, formUrl: formUrl, authToken: authToken, includeAttachments: true });

        this.service.exportFormToPdf(input)
            .finally(() => { this.endTask(); })
            .subscribe((result) => {
                if (result == null || result.fileName === '') {
                    return;
                }

                this._fileDownloadService.downloadTempFile(result);
            }, error => console.log('downloadFile', 'Could not download file.'));
    }

最后在方法中,添加转换器 HttpCookies 选项:

HtmlToPdf converter = new HtmlToPdf();
converter.Options.HttpCookies.Add("Abp.AuthToken", authToken);
PdfDocument doc = converter.ConvertUrl(url);
doc.Save(file);
doc.Close();

在此之后,我成功地转换了网址。

工作组

于 2018-09-18T23:43:43.413 回答
1

你看过这个页面吗? https://selectpdf.com/docs/WebPageAuthentication.htm

所有转换都在新会话中完成,因此您需要对转换器的用户进行身份验证。

于 2018-09-17T06:09:48.223 回答