ガットのヘルプ:
Is it possible to transfer authentication from Webbrowser to WebRequest
Alkampferは解決策を掲載しました。これはまさに私が必要としていたものです。
この解決方法には、Http only
クッキーが必要です。
WebRequestオブジェクトで後続の呼び出しに使用できるCookieContainerを返すGetUriCookieContainerメソッドを呼び出すことができます。
[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;
}
CookieがHttpOnlyのCookieのある場合、ウェブブラウザは、それらを格納しません。 [IEGetProtectedModeCookie](https://msdn.microsoft.com/en-us/library/cc196998(v=vs.85).aspx)を参照してください。 –