简体   繁体   English

使用WebBrowser控件和代理发送身份验证

[英]Sending authentication with WebBrowser control and proxy

I am using .net WebBrowser for web browsing in my application, as I know the control is base on IE therefore to set a proxy I was looking how to configure the IE proxy settings. 我在我的应用程序中使用.net WebBrowser进行Web浏览,因为我知道控件基于IE因此设置代理我正在寻找如何配置IE代理设置。 Came up with pinvoke InternetSetOption with following code: 想出了带有以下代码的pinvoke InternetSetOption:

private void SetIESettings(string proxy)
{
    const int INTERNET_OPTION_PROXY = 38;
    const int INTERNET_OPEN_TYPE_PROXY = 3;

    Struct_INTERNET_PROXY_INFO struct_IPI;

    struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
    struct_IPI.proxy = Marshal.StringToHGlobalAnsi(proxy);
    struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");

    IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
    Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
    InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
}

Works like a charm, after setting the proxy the control goes through it. 在设置代理后控件通过它就像魅力一样工作。 Something the proxy need credentials and before going through it a user name and pass need to be given. 代理需要凭证的东西,在通过它之前需要给出用户名和传递。 I tried to add additional header for the webBrowser.Navigate method like this: 我试图为webBrowser.Navigate方法添加额外的标头,如下所示:

var credentialStringValue = "user:password";
var credentialByteArray = ASCIIEncoding.ASCII.GetBytes(credentialStringValue);
var credentialBase64String = Convert.ToBase64String(credentialByteArray);
var credentials = string.Format("Authorization: Basic {0}{1}", credentialBase64String, Environment.NewLine);

then calling the navigation 然后调用导航

webBrowser.Navigate("http://google.com", "", null, credentials);

did not helped, still getting the authentication form. 没有帮助,仍然获得身份验证表单。 Also tried to add Proxy-Authorization header with same value, still nothing. 还尝试添加具有相同值的Proxy-Authorization标头,仍然没有。

Also tried to set the user and pass at internet explorer options as following: 还尝试设置用户并传递Internet Explorer选项,如下所示:

const int INTERNET_OPTION_USERNAME = 28;
const int INTERNET_OPTION_PASSWORD = 29;
var uesr = "user";
var pass = "password";
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_USERNAME, uesr, uesr.Length + 1);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PASSWORD, pass, pass.Length + 1);

Still not working, the security window keeps popping up. 仍然没有工作,安全窗口不断弹出。 Obviously I am doing something wrong, any suggestions? 显然我做错了什么,有什么建议吗?

Your direction is right. 你的方向是对的。 I met the issue when using IE comobject 我在使用IE comobject时遇到了这个问题

0x800C000E 0x800C000E

Following MS KB gives details why IE not supporting username and password in URL address. 以下MS KB详细说明了IE不支持URL地址中的用户名和密码的原因。 I tried to add the registry in the KB, solved my problem. 我试图在KB中添加注册表,解决了我的问题。

https://support.microsoft.com/en-us/help/834489/internet-explorer-does-not-support-user-names-and-passwords-in-web-sit https://support.microsoft.com/en-us/help/834489/internet-explorer-does-not-support-user-names-and-passwords-in-web-sit

在此输入图像描述

Create a registry, 创建一个注册表,

HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE HKEY_LOCAL_MACHINE \\ Software \\ Microsoft \\ Internet Explorer \\ Main \\ FeatureControl \\ FEATURE_HTTP_USERNAME_PASSWORD_DISABLE

Add a DWORD value "iexplore.exe" and data set to "0" 添加DWORD值“iexplore.exe”并将数据设置为“0”

Regarding your case, I think you can simply create a value pointing to your app and set data to "0" just like "iexplore.exe" and use the following URL format. 关于你的情况,我认为你可以简单地创建一个指向你的应用程序的值,并将数据设置为“0”,就像“iexplore.exe”一样,并使用以下URL格式。

https://username:password@xxx.yyy.zzz/home https://开头的用户名:password@xxx.yyy.zzz/home

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM