我正在尝试从Web应用程序中的WebBrowser控件转到HTTPS站点,以从站点获取一些基本信息(该站点此时没有Web服务或任何其他API) 当我从IIS Express执行此操作时,我能够连接登录并导航到其他页面,就像我从该系统上的Web浏览器直接连接时一切正常。
从大多数开发系统,即Windows 10或Windows Server 2016,我可以发布Web应用程序,连接到Web应用程序,然后通过Web应用程序连接到该站点,登录并加载其他页面一切正常。 但…。 当我部署到GoDaddy并通过应用程序连接到该站点时,我能够成功登录,但当我导航到另一个页面时,我被重定向到登录页面。
我注意到在GoDaddy上浏览应用程序时我没有收到任何JSESSION Cookies,但我确实在所有其他成功案例中都得到了它们。 我在登录目标站点之前收到了JSESSON Cookies:http://www.altavista.com/但是任何在连接上返回cookie的网站都应该足够了。 我已将用户代理更改为我在桌面上连接的相同代理并在GoDaddy上获得相同的结果。
我在受SSL保护的GoDaddy网站(HTTPS)和非Just(HTTP)的网站上尝试了这一点。 希望在使用WebBrowser控件之前有人遇到过这类问题?
这是代码的测试片段(你可能会注意到我已经尝试了很多东西来解决这个问题,解决CookiePresistance,清除Cookies,JScript以清除状态等等。
public static class NativeMethods
{
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption,
IntPtr lpBuffer, int lpdwBufferLength);
public static void SuppressCookiePersist()
{
int dwOption = 81; //INTERNET_OPTION_SUPPRESS_BEHAVIOR
int option = 3; // INTERNET_SUPPRESS_COOKIE_PERSIST
IntPtr optionPtr = Marshal.AllocHGlobal(sizeof(int));
Marshal.WriteInt32(optionPtr, option);
bool x = InternetSetOption(IntPtr.Zero, dwOption, optionPtr, sizeof(int));
Marshal.FreeHGlobal(optionPtr);
}
public static void EnableCookiePersist()
{
int dwOption = 81; //INTERNET_OPTION_SUPPRESS_BEHAVIOR
int option = 4; // INTERNET_SUPPRESS_COOKIE_POLICY ignores policy and allows cookies to be set https://msdn.microsoft.com/en-us/library/windows/desktop/aa385328(v=vs.85).aspx
IntPtr optionPtr = Marshal.AllocHGlobal(sizeof(int));
Marshal.WriteInt32(optionPtr, option);
InternetSetOption(IntPtr.Zero, dwOption, optionPtr, sizeof(int));
option = 1;
Marshal.WriteInt32(optionPtr, option);
InternetSetOption(IntPtr.Zero, dwOption, optionPtr, sizeof(int));
Marshal.FreeHGlobal(optionPtr);
}
public static void ClearBrowserSession()
{
int dwOption = 42; //INTERNET_OPTION_END_BROWSER_SESSION
InternetSetOption(IntPtr.Zero, dwOption, IntPtr.Zero, 0);
}
}
public partial class TestStub: System.Web.UI.Page
{
protected void btnNavigate_Click(object sender, EventArgs e)
{
NativeMethods.EnableCookiePersist();
if(tbUrl.Text == "")
{
Client_Alert("Please enter url");
return;
}
if (tbUserAgent.Text == "")
tbUserAgent.Text = CurrentUserAgent;
object o = tbUrl.Text;
var t = new Thread((ParameterizedThreadStart)GetPage);
t.SetApartmentState(ApartmentState.STA);
t.Start(o);
GettingPage = true;
while (GettingPage == true)
Thread.Sleep(500);
t.Join();
return;
}
private void GetPage(object o)
{
StringBuilder SB = new StringBuilder();
DirectorBrowser = new WebBrowser();
NativeMethods.ClearBrowserSession();
DirectorBrowser.ScrollBarsEnabled = false;
DirectorBrowser.ScriptErrorsSuppressed = true;
DirectorBrowser.AllowNavigation = true;
DirectorBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; '); for (e = 0; e < a.length && a[e]; e++) { f++; for (b = '.' + location.host; b; b = b.replace(/^ (?:% 5C.|[^% 5C.] +) /, '')){ for (c = location.pathname; c; c = c.replace(/.$/, '')) { document.cookie = (a[e] + '; domain=' + b + '; path=' + c + '; expires=' + new Date((new Date()).getTime() - 1e11).toGMTString()); } }}})())");
NativeMethods.EnableCookiePersist();
DirectorBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_NoticeCompleted);
Uri url = new Uri((string)o);
DirectorBrowser.Navigate(url, null, null, "User-Agent: " + tbUserAgent.Text);
GettingPage = true;
while (GettingPage == true)
{
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(500);
}
}
private void browser_NoticeCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (GettingPage == true)
{
HtmlElement body = DirectorBrowser.Document.Body;
HtmlElementCollection inputs = DirectorBrowser.Document.GetElementsByTagName("INPUT");
HtmlElementCollection forms = DirectorBrowser.Document.Forms;
body = DirectorBrowser.Document.Body;
string webResults = body.InnerHtml;
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(webResults);
htmlDoc.OptionFixNestedTags = true;
tbMaintenanceResults.Text = "Cookies: <br>" + DirectorBrowser.Document.Cookie + "<br>" + body.InnerHtml;
GettingPage = false;
}
else
{
GettingPage = false;
}
}
}
答案 0 :(得分:0)
添加自定义安全管理器后,仍然没有成功:
public partial class InternetSecurityManager : IInternetSecurityManager
{
private static Guid _CLSID_SecurityManager = new Guid("7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4");
private static string[] ZoneNames = new[] { "Local", "Intranet", "Trusted", "Internet", "Restricted" };public static string GetUrlZone(string url)
{
Type t = System.Type.GetTypeFromCLSID(_CLSID_SecurityManager);
IInternetSecurityManager securityManager = (IInternetSecurityManager)System.Activator.CreateInstance(t);
try
{
uint zone = 0;
int hResult = securityManager.MapUrlToZone(url, ref zone, 0);
if (hResult != 0)
throw new COMException("Error calling MapUrlToZone, HRESULT = " + hResult.ToString("x"), hResult);
if (zone < ZoneNames.Length)
return ZoneNames[zone];
return "Unknown - " + zone;
}
finally
{
Marshal.ReleaseComObject(securityManager);
}
}
public static void GetUrlZoneCookiesPermissions(string url)
{
Type t = System.Type.GetTypeFromCLSID(_CLSID_SecurityManager);
IInternetSecurityManager securityManager = (IInternetSecurityManager)System.Activator.CreateInstance(t);
try
{
uint puaf = 0x00000040; //PUAF TRUSTED
byte obptr = 0;
int hResult = securityManager.ProcessUrlAction(url, 0x00001A06, out obptr, 1, 0, 0, puaf, 0);
//hResult = securityManager.ProcessUrlAction(url, 0x00001A10, out obptr, 1, 0, 0, puaf, 0);
hResult = securityManager.ProcessUrlAction(url, 0x00001A03, out obptr, 1, 0, 0, puaf, 0);
return;
}
finally
{
Marshal.ReleaseComObject(securityManager);
}
}
[return: MarshalAs(UnmanagedType.I4)]
public int SetSecuritySite([In] IntPtr pSite)
{
return Convert.ToInt32( 0x800C0011); //INET_E_DEFAULT_ACTION;
}
[return: MarshalAs(UnmanagedType.I4)]
public int GetSecuritySite([Out] IntPtr pSite)
{
return Convert.ToInt32(0x800C0011); //INET_E_DEFAULT_ACTION;
}
[return: MarshalAs(UnmanagedType.I4)]
public int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref uint pdwZone, uint dwFlags)
{
return Convert.ToInt32(0x800C0011); //INET_E_DEFAULT_ACTION;
}
[return: MarshalAs(UnmanagedType.I4)]
public int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId, ref uint pcbSecurityId, uint dwReserved)
{
return Convert.ToInt32(0x800C0011); //INET_E_DEFAULT_ACTION;
}
[return: MarshalAs(UnmanagedType.I4)]
public int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, uint dwAction, out byte pPolicy, uint cbPolicy, byte pContext, uint cbContext, uint dwFlags, uint dwReserved)
{
pPolicy = 0;
return 0;// S_OK;
}
[return: MarshalAs(UnmanagedType.I4)]
public int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref Guid guidKey, ref byte ppPolicy, ref uint pcbPolicy, ref byte pContext, uint cbContext, uint dwReserved)
{
return Convert.ToInt32(0x800C0011); //INET_E_DEFAULT_ACTION;
}
[return: MarshalAs(UnmanagedType.I4)]
public int SetZoneMapping(uint dwZone, [In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, uint dwFlags)
{
return Convert.ToInt32(0x800C0011); //INET_E_DEFAULT_ACTION;
}
[return: MarshalAs(UnmanagedType.I4)]
public int GetZoneMappings(uint dwZone, out UCOMIEnumString ppenumString, uint dwFlags)
{
ppenumString = null;
return Convert.ToInt32(0x800C0011); //INET_E_DEFAULT_ACTION;
}
}
[ComImport, GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInternetSecurityManager
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int SetSecuritySite([In] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetSecuritySite([Out] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
ref UInt32 pdwZone, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
[MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId,
ref UInt32 pcbSecurityId, uint dwReserved);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
UInt32 dwAction, out byte pPolicy, UInt32 cbPolicy,
byte pContext, UInt32 cbContext, UInt32 dwFlags,
UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
ref Guid guidKey, ref byte ppPolicy, ref UInt32 pcbPolicy,
ref byte pContext, UInt32 cbContext, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int SetZoneMapping(UInt32 dwZone,
[In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern,
UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetZoneMappings(UInt32 dwZone, out UCOMIEnumString ppenumString,
UInt32 dwFlags);
}
}
我在2006年发现这篇文章:http://www.tech-archive.net/Archive/InetSDK/microsoft.public.inetsdk.programming.webbrowser_ctl/2006-03/msg00073.html 显然,安全管理器不处理Cookie,它们由区域管理器处理,无法自定义。解决此问题的唯一方法是根据您的特定需求向注册表添加其他区域。不幸的是,当您使用WebBrowser控件时,无法通过代码执行此操作。如果您的站点在Web场中托管,则您无权访问注册表,因此需要另一种方法(可能是IE插件或部署在您可以访问的服务器上,并且可以将默认浏览器设置为您需要)