ASPNETZERO - SelectPdf中的身份验证ConvertUrl()来自Angular 4 / .Net Core

问题描述 投票:1回答: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();

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

谢谢!

angular .net-core aspnetboilerplate selectpdf
2个回答
1
投票

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

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


1
投票

让我在身份验证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值,而是在call参数中传递了它:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.