這些都沒有工作。由於Windows安全功能,它將始終彈出用戶名和密碼對話框。您首先必須將憑據存儲在Windows憑據中。你需要做的第一件事是通過NuGet包管理器下載'CredentialManagment'包。您首先必須使用用戶名和密碼將代理信息存儲在註冊表中。下面是註冊表
[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);
//<-loopback>;<local>
Registry.SetValue(keyName, "ProxyOverride", "*.local", RegistryValueKind.String);
// These lines implement the Interface in the beginning of program
// They cause the OS to refresh the settings, causing IP to realy update
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
,那麼你需要設置憑據
Credential credentials= new Credential
{
Username = "Usernmae",
Password = "Password",
Target = "Target (usualy proxy domain)",
Type = CredentialType.Generic,
PersistanceType = PersistanceType.Enterprise
};
credentials.Save();
我用這與.NET 4.5.2
@Abat代碼 - 當然 - 沒有人。下一次你做一個雙重編輯,只需在編輯中添加理由。 – 2011-05-31 08:48:01
@凱爾:當然!乾杯! :) – abatishchev 2011-05-31 09:09:22