None of them are going to work. Due to the security features of Windows, a username and password dialog will always be displayed. You must first save the credentials in the Windows credentials. the first thing you need to do is download the CredentialManagment package through the NuGet package manager. First you need to save the proxy information in the registry with a username and password. Here is the code for the registry
[DllImport("wininet.dll", SetLastError = true)] public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); public const int INTERNET_OPTION_SETTINGS_CHANGED = 39; public const int INTERNET_OPTION_REFRESH = 37; static void setProxyRegistry(string proxyhost, bool proxyEnabled, string username, string password) { const string userRoot = "HKEY_CURRENT_USER"; const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"; const string keyName = userRoot + "\\" + subkey; Registry.SetValue(keyName, "ProxyServer", proxyhost, RegistryValueKind.String); Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord); Registry.SetValue(keyName, "ProxyPass", password, RegistryValueKind.String); Registry.SetValue(keyName, "ProxyUser", username, RegistryValueKind.String);
then you need to set credentials
Credential credentials= new Credential { Username = "Usernmae", Password = "Password", Target = "Target (usualy proxy domain)", Type = CredentialType.Generic, PersistanceType = PersistanceType.Enterprise }; credentials.Save();
I use this with .NET 4.5.2
Seven
source share