2017-10-16 101 views
1

我希望能夠使用.NET Identity在我的C#.NET MVC應用程序中的操作中呈現PDF。如何在HiQPdf中使用.NET Identity並呈現PDF認證

但是,我想渲染的動作需要用戶進行身份驗證,並且HiQPdf不會像原來那樣尊重Cookie。

我該如何做到這一點?

+1

只是爲了阻止更多的萎靡不振:**自應答不僅是允許的,但[特別鼓勵(HTTPS:/ /stackoverflow.com/help/self-answer)**。請不要標記此類帖子以供版主注意,也不要因爲使用了專門用於分享知識的功能而對作者反省。 –

回答

1

您需要的cookie傳遞到PDF生成器,其餘的應該工作:

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); 
} 
相關問題