📜 ⬆️ ⬇️

Using ClickOnce and Proxy

ClickOnce - Microsoft technology for deploying applications. ClickOnce applications can check for new versions as they become available and automatically replace any updated files, which is very convenient.

It so happens that a proxy server (with or without authentication) is used to control user access to the Internet and external network resources. ClickOnce provides support for integrated proxy authentication in Windows starting in the .NET Framework 3.5, but does not support other authentication protocols, such as Basic Authentication or Brief Authentication.

If authentication is not used, we can configure the proxy using the defaultProxy element:

<defaultProxy enabled="true|false" useDefaultCredentials="true|false" <bypasslist></bypasslist> <proxy></proxy> <module></module> /> 

But if the computer is configured to use a proxy server that requires authentication, we will receive the following error when attempting to update, even if we specify our credentials:
The remote server returned an error: (407) proxy authentication is required.

This is because every time a file is loaded using the SystemNetDownloader class, credentials are reset to default data.
')
We solve this problem as follows:

Create your own proxy class: CustomUserProxy
  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); } /// <summary> ///  ,   Credentials /// </summary> /// <param name="credentials">   </param> public void SetCredentials(ICredentials credentials) { _webProxy.Credentials = credentials; } } 


Use your proxy:
 Uri uriProxy = new Uri("http://myproxy.ent:808"); CustomUserProxy customProxy = new CustomUserProxy(uriProxy); customProxy.SetCredentials(new NetworkCredential("Login", "Password")); WebRequest.DefaultWebProxy = customProxy; 


Works in a project that uses ClickOnce updates and calls to WCF services.

Source: https://habr.com/ru/post/279879/


All Articles