Using ClickOnce and a proxy server
It so happens that to control user access to the Internet and external network resources, a proxy server is used (with or without authentication). ClickOnce provides support for integrated proxy authentication on Windows starting with the .NET Framework 3.5, but does not support other authentication protocols, such as Basic Authentication or Basic Authentication.
If authentication is not used, we can configure the proxy using the defaultProxy element :
…
… … />
But if the computer is configured to use a proxy server that requires authentication, we will receive the following error when trying to update, even if we indicate our Credentials:
The remote server returned an error: (407) Proxy authentication required.
This is because every time a file is downloaded using the SystemNetDownloader class , credentials are reset to default data.
We solve this problem as follows:
public class CustomUserProxy : IWebProxy
{
private WebProxy _webProxy;
public CustomUserProxy(Uri uri)
{
_webProxy = new WebProxy(uri);
}
public ICredentials Credentials
{
get { return _webProxy.Credentials; }
set { }
}
public Uri GetProxy(Uri destination)
{
return _webProxy.GetProxy(destination);
}
public bool IsBypassed(Uri host)
{
return _webProxy.IsBypassed(host);
}
///
/// Альтернативный метод, который устанавливает Credentials
///
/// Учетные данные для аутентификации
public void SetCredentials(ICredentials credentials)
{
_webProxy.Credentials = credentials;
}
}
Uri uriProxy = new Uri("http://myproxy.ent:808");
CustomUserProxy customProxy = new CustomUserProxy(uriProxy);
customProxy.SetCredentials(new NetworkCredential("Login", "Password"));
WebRequest.DefaultWebProxy = customProxy;
He works in a project that uses ClickOnce updates and calls to WCF services.