c# – 模拟ASP.NET身份验证cookie

前端之家收集整理的这篇文章主要介绍了c# – 模拟ASP.NET身份验证cookie前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我维护一个使用的ASP.NET MVC网站
FormsAuthentication.SetAuthCookie(userName,createPersistentCookie);

来签名用户(最终得到一个名为.ASPXAUTH的cookie).

客户希望我添加一个HTML到PDF功能,所以我正在包装wkhtmltopdf库,并呼吁.这最终是一个看起来像这样的命令:

wkhtmltopdf http://example.com/Foo/Edit/42 Foo.pdf

但是,由于wkhtmltopdf用户代理程序没有正确的cookie,因此将导致登录屏幕的PDF.

这很好,根据wkhtmltopdf文档,有一个这样的参数:

--cookie <name> <value>         Set an additional cookie (repeatable)

所以我修改命令是:

wkhtmltopdf --cookie .ASPXAUTH 91C0DE4C...  http://example.com/Foo/Edit/42 Foo.pdf

使用Request.Cookie [“.ASPXAUTH”]获取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,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

解决方法

我发现了这个问题.我注意到,一旦我将User-Agent字段(在Fiddler中)更改为与Chrome相同,它的工作正常.所以我做了一个小网络,并发现了 this bug on the wkhtmltopdf project page.

错误

This is an issue under ASP .NET 4.0 as it seems that .NET interprets
the User-Agent string “Mozilla/5.0 (Windows; U; Windows NT 6.1; en-AU)
AppleWebKit/532.4 (KHTML,like Gecko) Qt/4.6.1 Safari/532.4” as not
supporting cookies which I think is preventing the –cookie option
from working under ASP.

所以看起来解决方案是想出一个方法,使wkhtmltopdf改变它的用户代理头(不看好看),或者找出一种告诉ASP.NET该用户代理支持cookie的方法.

谢谢你帮助Darin Dimitrov.

更新

好的,我想出了如何告诉ASP.NET wkhtmltopdf使用的Qt Web浏览器支持cookies.您需要创建一个名为qt.browser的文件,并将其保存在ASP.NET项目根目录中的callde App_Browsers目录中.这是你放在qt.browser文件中:

<browsers>
    <!-- Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/530.1 (KHTML,like Gecko) Qt/4.7.1 Safari/530.1 -->
    <browser id="Qt" parentID="Safari">
        <identification>
            <userAgent match="Qt/(?'version'(?'major'\d+)(\.(?'minor'\d+)?)\w*)" />
        </identification>       

        <capabilities>
            <capability name="browser"                         value="Qt" />
            <capability name="version"                         value="${version}" />
            <capability name="majorversion"                    value="${major}" />
            <capability name="minorversion"                    value="${minor}" />
            <capability name="type"                            value="Qt${major}" />
            <capability name="ecmascriptversion"               value="3.0" />
            <capability name="javascript"                      value="true" />
            <capability name="javascriptversion"               value="1.7" />
            <capability name="w3cdomversion"                   value="1.0" />
            <capability name="tagwriter"                       value="System.Web.UI.HtmlTextWriter" />
            <capability name="cookies"                         value="true" />
            <capability name="frames"                          value="true" />
            <capability name="javaapplets"                     value="true" />
            <capability name="supportsAccesskeyAttribute"      value="true" />
            <capability name="supportsCallback"                value="true" />
            <capability name="supportsDivNoWrap"               value="false" />
            <capability name="supportsFileUpload"              value="true" />
            <capability name="supportsMaintainScrollPositionOnPostback" value="true" />
            <capability name="supportsMultilineTextBoxDisplay" value="true" />
            <capability name="supportsXmlHttp"                 value="true" />
            <capability name="tables"                          value="true" />
        </capabilities>
    </browser>
</browsers>

然后重新编译你的项目(也许重新启动你的服务器,如果可以)然后presto,你可以模仿ASP.NET认证cookie!

原文链接:https://www.f2er.com/csharp/97111.html

猜你在找的C#相关文章