2

I am using ASPNETZERO's Angular 4 + .Net Core.

I have a grid that displays a list of the user's submitted forms and a column with a button to print the form.

Here's my print function; I'm passing the url for the ConvertUrl() method in the input:

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

Everything is working fine with the process of converting and downloading the file, however, when I do the convert (below) the url redirects to the login page because of authentication and it's that page being converted.

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

I don't know how to use SelectPdf's authentication options with ASPNETZERO and am hoping that someone knows of a way that I can pass the current session/credentials or how to use one of SelectPdf's authentication options so it converts the passed url.

Thank!

Wg

Wizgod
  • 91
  • 10
  • Is your conversion and download function required user login? – ryan.c Sep 16 '18 at 10:30
  • Yes; the user needs to be logged in to access their list of forms and use the print/export to PDF functionality. – Wizgod Sep 16 '18 at 18:53
  • Does the user who can access the export and download functionality can access the selected form as well? Login redirection only happens if the current user didn't not login and looks like it isn't this for your case. – ryan.c Sep 16 '18 at 23:24
  • Yes, they have access to the form; the issue is when executing SelectPdf's ConvertUrl(url) method (added the convert code to the question). – Wizgod Sep 17 '18 at 05:42

2 Answers2

2

What threw me off in the SelectPdf documentation for the authentication cookie was the System.Web.Security.FormsAuthentication.FormsCookieName in the example which I assumed was what it should be.

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

but I got the following exception:

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

What I finally realized was that I needed to pass the ASPNETZERO authentication cookie (which after looking in the cookie folder was "Abp.AuthToken"). Instead of trying to get the cookie value in the service method, I passed it in the call parameter:

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

and finally in the method, adding the converter HttpCookies option:

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

After this, I was successfully able to convert the url.

Wg

Wizgod
  • 91
  • 10
1

Have you seen this page? https://selectpdf.com/docs/WebPageAuthentication.htm

All conversions are done in a new session, so you need to authenticate the user for the converter.

Tom
  • 285
  • 2
  • 3
  • Hi Tom; yes, I have looked at that but I don't know how to incorporate it with ASPNETZERO's login; I have tried the various options with no luck. – Wizgod Sep 17 '18 at 16:35