How can I use .NET Identity with HiQPdf and render a PDF authenticated

442 Views Asked by At

I want to be able to render a PDF from an action in my C#.NET MVC application using .NET Identity.

However, the Action I want to render requires the user to be authenticated and HiQPdf doesn't respect cookies as-is.

How can I achieve this?

1

There are 1 best solutions below

0
karlingen On

You need to pass cookies to the pdf generator and the rest should work:

public ActionResult DownloadPdf(int? id)
{
    var fullUrl = Url.Action("Details", "MyController", new { id }, Request.Url.Scheme);
    var pdf = new HtmlToPdf();
    string fileName = "Some name.pdf";
    var cookies = Request.Cookies.AllKeys.ToDictionary(k => k, k => Request.Cookies[k].Value);
    foreach(var cookie in cookies)
        pdf.HttpCookies.AddCookie(cookie.Key,cookie.Value);

    return File(pdf.ConvertUrlToMemory(fullUrl), System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
}