wkhtmltopdf登录到asp.net网络应用程序

Dan*_*abo 8 asp.net login wkhtmltopdf

遇到wkhtmltopdf的问题.我正在使用它在具有用户名/密码页面的网站上拍摄页面的pdf快照.当.exe运行时,我最终得到了登录页面的快照(从我自己的ASP.NET应用程序运行exe).

有没有人知道如何让wkhtmltopdf登录网站,以便它可以访问所需的页面快照?

wkhtmltopdf安装在服务器上的程序文件目录中,并通过以下方式调用:

public void HtmlToPdf(string website, string destinationFile)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "wkhtmltopdf.exe";
        startInfo.Arguments = website + " " + destinationFile;
        Process.Start(startInfo);
    }
Run Code Online (Sandbox Code Playgroud)

谢谢! - 担


答案

我无法使用--cookie-jar方法(请参阅注释),但我确实找到了另一种以编程方式使用查询字符串中的用户名/密码登录的方法.

我将用户名/ pw作为我的查询字符串中的params传递,并尝试使用wkhtml访问我想要的页面.当成员资格提供者将我踢到登录页面时,我通过代码隐藏访问params(作为returnUrl param存储在url中)并对自己进行身份验证.一个简单的response.redirect和宾果 - 我有我的PDF快照.

// Check to see if an outside program is trying
// to log in by passing creds in the querystring.
if (Request.QueryString["username"] != null) &&
    Request.QueryString["password"] != null))
{ 
    string user = Request.QueryString["username"];
    string pw   = Request.QueryString["password"];
    if (System.Web.Security.Membership.ValidateUser(user, pw))
    {
        // Create an authentication ticket for wkhtml session
        System.Web.Security.FormsAuthentication.SetAuthCookie(user, false);
        if (Request.QueryString["ReturnUrl"] != null)
        {
            Response.Redirect(Request.QueryString["ReturnUrl"]);
        }
    }
    else 
    {
        throw new Exception("You failed to log in.");
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 2

首先,检查登录表单使用的 post 参数,然后尝试 --post 用户名 xxx --post 密码 xxx。或者使用wireshark并记录登录过程并查看发布了哪些参数。

一旦你登录使用--cookie-jar

在这里查看更好的解释http://wkhtmltopdf.org/

让 wkhtmltopdf 转换受保护的页面可能很棘手。还可以使用 --extended-help 查看可用于登录的其他参数。例如,如果站点受基本身份验证保护,则使用 --user --password 应该相当容易