C# 获取网络浏览器 cookie 以登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15049877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting webbrowser cookies to log in
提问by Jaanus
I am creating an windows forms app, where I have a webbrowser control
.
我正在创建一个 Windows 窗体应用程序,其中有一个webbrowser control
.
After user logs in with the webbrowser
, I want to log in also with same account with Microsoft.Http.HttpClient
or HttpWebRequest
or similar, it should be similar to cURL
from PHP
.
用户使用 登录后webbrowser
,我也想使用Microsoft.Http.HttpClient
或HttpWebRequest
或类似的相同帐户登录,应该类似于cURL
from PHP
。
Problem is that the webpage allows only single sign on per account and if I sign with HttpClient, it will kick the webbrowser out.
问题是该网页只允许每个帐户单点登录,如果我使用 HttpClient 签名,它会将网络浏览器踢出。
What I want to know, if it is possible to hiHyman webbrowser
session or get the cookies
and use it in my HttpClient
or similar api
.
我想知道的是,是否有可能劫持webbrowser
会话或获取cookies
并在我的HttpClient
或类似的api
.
I can use webbrowser.Cookie
to get some data, but how to push it to HttpClient
?
我可以webbrowser.Cookie
用来获取一些数据,但如何将其推送到HttpClient
?
Is that kind of thing even possible, that I can just take the cookies and use the same session? If so, how?
这种事情甚至可能吗,我可以只拿饼干并使用相同的会话?如果是这样,如何?
采纳答案by Jaanus
Got help from here:
从这里得到帮助:
Is it possible to transfer authentication from Webbrowser to WebRequest
是否可以将身份验证从 Webbrowser 传输到 WebRequest
Alkampfer posted solution. This is exactly what I needed and it worked.
Alkampfer 发布了解决方案。这正是我所需要的,并且奏效了。
This solution also takes Http only
cookies.
此解决方案也需要Http only
cookie。
You can call the GetUriCookieContainer method that returns you a CookieContainer that can be used for subsequent call with WebRequest object.
您可以调用 GetUriCookieContainer 方法,该方法返回一个 CookieContainer,可用于后续调用 WebRequest 对象。
[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetGetCookieEx(
string url,
string cookieName,
StringBuilder cookieData,
ref int size,
Int32 dwFlags,
IntPtr lpReserved);
private const Int32 InternetCookieHttponly = 0x2000;
/// <summary>
/// Gets the URI cookie container.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns></returns>
public static CookieContainer GetUriCookieContainer(Uri uri)
{
CookieContainer cookies = null;
// Determine the size of the cookie
int datasize = 8192 * 16;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(
uri.ToString(),
null, cookieData,
ref datasize,
InternetCookieHttponly,
IntPtr.Zero))
return null;
}
if (cookieData.Length > 0)
{
cookies = new CookieContainer();
cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
}
return cookies;
}
回答by Jorge Alvarado
I think you might be stretching the technologies a little bit too far. The webbrowser control in windows forms are usually designed to give you basic html rendering from local files or internet, but it shouldn't be used to replace a full-fledge web browser.
我认为您可能将这些技术延伸得太远了。Windows 窗体中的 webbrowser 控件通常旨在为您提供来自本地文件或 Internet 的基本 html 呈现,但它不应用于替换成熟的 web 浏览器。
If you want to authenticate against a SSO provider, then you have to use the right libraries, in this case from Windows Identitity Foundation, with Microsoft.IdentityModel you will get claims authentication mechanisms to handle the claims from your SSO provider. Cookies cannot be shared across applications, in fact some new web technologies are designed just to avoid that, so in my opinion try to use WIF instead of a webbrowser control.
如果您想针对 SSO 提供程序进行身份验证,那么您必须使用正确的库,在这种情况下来自 Windows Identity Foundation,通过 Microsoft.IdentityModel,您将获得声明身份验证机制来处理来自您的 SSO 提供程序的声明。Cookie 不能跨应用程序共享,事实上一些新的网络技术就是为了避免这种情况而设计的,所以在我看来,尝试使用 WIF 而不是网络浏览器控件。
Hope it helps,
希望能帮助到你,
回答by ThomasArdal
You can, but it's a bit tricky. Use the InternetSetCookie call together with a CookieContainer from HttpWebRequest.
你可以,但它有点棘手。将 InternetSetCookie 调用与来自 HttpWebRequest 的 CookieContainer 一起使用。
Here's the method: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385107(v=vs.85).aspx
这是方法:http: //msdn.microsoft.com/en-us/library/windows/desktop/aa385107(v=vs.85).aspx
and here's an example on how to set it: http://social.msdn.microsoft.com/Forums/en-SG/csharpgeneral/thread/76a38eee-3ef6-4993-a54d-3fecc4eb6cff
这里有一个关于如何设置它的例子:http: //social.msdn.microsoft.com/Forums/en-SG/csharpgeneral/thread/76a38eee-3ef6-4993-a54d-3fecc4eb6cff
回答by ihorbond
I do it this way
我这样做
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(address);
var cookies = new CookieContainer();
cookies.SetCookies(webBrowser.Document.Url,
webBrowser.Document.Cookie.Replace(';', ','));
req.CookieContainer = cookies;