Search code examples
angular.net-coreaspnetboilerplateselectpdf

ASPNETZERO - Authentication in SelectPdf ConvertUrl() From Angular 4/.Net Core


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


Solution

  • 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