模拟 ASP.NET 身份验证 cookie
我维护一个 ASP.NET MVC 网站,用于
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
登录用户(他们最终会得到一个名为 .ASPXAUTH
的 cookie)。
客户希望我将 HTML 添加到 PDF 功能,因此我封装了 wkhtmltopdf 库并称之为。这最终是一个如下所示的命令:
wkhtmltopdf http://example.com/Foo/Edit/42 Foo.pdf
但是,这会导致生成登录屏幕的 PDF,因为 wkhtmltopdf 用户代理由于没有正确的 cookie 而被重定向。
这很好,因为根据 wkhtmltopdf 文档,有一个这样的参数:
--cookie <name> <value> Set an additional cookie (repeatable)
所以我将命令修改为:
wkhtmltopdf --cookie .ASPXAUTH 91C0DE4C... http://example.com/Foo/Edit/42 Foo.pdf
使用 Request.Cookie[".ASPXAUTH"].Value 检索 cookie 值。
不幸的是,这似乎不起作用,我不知道为什么。我知道 ASP.NET 正在接收 cookie,因为当我在重定向后对登录页面进行断点时,我可以看到它已被设置。那么为什么 ASP.NET 不接受我复制的 cookie?
以下是 ASP.NET 允许的请求内容(来自 Chrome):
GET http://localhost:50189/ReportingMonth/Edit/1193391 HTTP/1.1
Host: localhost:50189
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-CA,en;q=0.8,en-US;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: .ASPXAUTH=C8189041BF69FEF89A834B6F5035B786EC40145FFFBA3DBB6A04973BC58021C73D8D374E3577AA44BC26A784BC8A0C24831CF49FBD596BFFBA42C613E3C2C0C893D1587B7743D051643088BB8BAB667C047E0D1B84D7B76C4AADA7C62AB460D87C954BF9118BF5945E7D325D455CFD13A34C3DD5E597AFDF75D3C8EE76D8488B08ABBF6AE065B4C57CE47CB65AB17D65; language=en; ui-tabs-[object Object]=0
这是它重定向到登录的请求内容(来自 wkhtmltopdf):
GET http://localhost:50189/ReportingMonth/Edit/1193391 HTTP/1.1
Cookie: .ASPXAUTH=C8189041BF69FEF89A834B6F5035B786EC40145FFFBA3DBB6A04973BC58021C73D8D374E3577AA44BC26A784BC8A0C24831CF49FBD596BFFBA42C613E3C2C0C893D1587B7743D051643088BB8BAB667C047E0D1B84D7B76C4AADA7C62AB460D87C954BF9118BF5945E7D325D455CFD13A34C3DD5E597AFDF75D3C8EE76D8488B08ABBF6AE065B4C57CE47CB65AB17D65
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.3 (KHTML, like Gecko) Qt/4.7.1 Safari/533.3
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en-US,*
Host: localhost:50189
I maintain an ASP.NET MVC website that uses
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
to sign users in (they end up with a cookie named .ASPXAUTH
).
The client wants me to add an HTML to PDF feature, so I'm wrapping the wkhtmltopdf library and calling that. This ends up being a command that looks like this:
wkhtmltopdf http://example.com/Foo/Edit/42 Foo.pdf
However, that results in making a PDF of the login screen, as the wkhtmltopdf user agent is redirected since it doesn't have the correct cookie.
That's fine since, according to the wkhtmltopdf documentation, there's an argument like this:
--cookie <name> <value> Set an additional cookie (repeatable)
So I modify the command to be:
wkhtmltopdf --cookie .ASPXAUTH 91C0DE4C... http://example.com/Foo/Edit/42 Foo.pdf
Where the cookie value is retrieved using Request.Cookie[".ASPXAUTH"].Value
.
Unfortunately, this doesn't seem to work and I have no idea why. I know that ASP.NET is receiving the cookie because when I breakpoint the login page after the redirect, I can see that it's been set. So why does ASP.NET not accept my copied cookie?
Here's the contents of a request that ASP.NET allows (from Chrome):
GET http://localhost:50189/ReportingMonth/Edit/1193391 HTTP/1.1
Host: localhost:50189
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-CA,en;q=0.8,en-US;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: .ASPXAUTH=C8189041BF69FEF89A834B6F5035B786EC40145FFFBA3DBB6A04973BC58021C73D8D374E3577AA44BC26A784BC8A0C24831CF49FBD596BFFBA42C613E3C2C0C893D1587B7743D051643088BB8BAB667C047E0D1B84D7B76C4AADA7C62AB460D87C954BF9118BF5945E7D325D455CFD13A34C3DD5E597AFDF75D3C8EE76D8488B08ABBF6AE065B4C57CE47CB65AB17D65; language=en; ui-tabs-[object Object]=0
And here's one that it redirects to the login (from wkhtmltopdf):
GET http://localhost:50189/ReportingMonth/Edit/1193391 HTTP/1.1
Cookie: .ASPXAUTH=C8189041BF69FEF89A834B6F5035B786EC40145FFFBA3DBB6A04973BC58021C73D8D374E3577AA44BC26A784BC8A0C24831CF49FBD596BFFBA42C613E3C2C0C893D1587B7743D051643088BB8BAB667C047E0D1B84D7B76C4AADA7C62AB460D87C954BF9118BF5945E7D325D455CFD13A34C3DD5E597AFDF75D3C8EE76D8488B08ABBF6AE065B4C57CE47CB65AB17D65
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.3 (KHTML, like Gecko) Qt/4.7.1 Safari/533.3
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en-US,*
Host: localhost:50189
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了问题。我注意到,一旦我将 User-Agent 字段(在 Fiddler 中)更改为与 Chrome 相同,它就可以正常工作。因此,我做了一些网络调查,发现了wkhtmltopdf 项目页面上的这个错误< /a>.
从错误来看:
因此,看起来解决方案要么找到一种方法让 wkhtmltopdf 更改它的
User-Agent
标头(看起来不太有希望),要么找到一种方法告诉 ASP.NET 该用户代理确实支持 cookie 。感谢您帮助达林·季米特洛夫。
更新
好的,我想出了如何告诉 ASP.NET wkhtmltopdf 使用的 Qt Web 浏览器支持 cookie。您需要创建一个名为
qt.browser
的文件,并将其保存在 ASP.NET 项目根目录中名为App_Browsers
的目录中。以下是您在qt.browser
文件中放入的内容:然后重新编译您的项目(如果可以的话,也许重新启动您的服务器),然后您就可以模拟 ASP.NET 身份验证 cookie!
I found the problem. I noticed that once I changed the
User-Agent
field (in Fiddler) to be the same as Chrome, it worked fine. So I did a little web sleuthing and discovered this bug on the wkhtmltopdf project page.From the bug:
So it looks like the solution is either figuring out a way to make wkhtmltopdf change it's
User-Agent
header (not looking promising) or figure out a way to tell ASP.NET that that user agent does support cookies.Thanks for you help Darin Dimitrov.
Update
Ok, I figured out how to tell ASP.NET that the Qt web browser used by wkhtmltopdf supports cookies. You need to create a file called
qt.browser
and save it in a directory calldeApp_Browsers
in the root of your ASP.NET project. Here's what you put in theqt.browser
file:Then re-compile your project (and maybe restart your server if you can) and then presto, you can emulate the ASP.NET authentication cookie!
看起来像一个错误,并且似乎存在固定在后备箱。
Looks like a bug and there seems to be a fix in the trunk.