Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Proxy.cs @ 84a200dc

History | View | Annotate | Download (2.6 kB)

1
// -----------------------------------------------------------------------
2
// <copyright file="Proxy.cs" company="Microsoft">
3
// TODO: Update copyright text.
4
// </copyright>
5
// -----------------------------------------------------------------------
6

    
7
using System.Net;
8
using Pithos.Interfaces;
9

    
10
namespace Pithos.Client.WPF
11
{
12
    using System;
13
    using System.Collections.Generic;
14
    using System.Linq;
15
    using System.Text;
16

    
17
    /// <summary>
18
    /// TODO: Update summary.
19
    /// </summary>
20
    public class Proxy
21
    {
22
        //or the proxy changes, not when the monitor starts
23
        public static void SetFromSettings(IPithosSettings pithosSettings)
24
        {
25
            IWebProxy proxy;
26
            if (pithosSettings.UseNoProxy)
27
            {
28
                proxy = null;
29
            }
30
            else if (pithosSettings.UseManualProxy)
31
            {
32
                proxy = new WebProxy(pithosSettings.ProxyServer, pithosSettings.ProxyPort);
33
                //If the proxy requires specific authentication settings, use them
34
                if (pithosSettings.ProxyAuthentication)
35
                {
36
                    proxy.Credentials = new NetworkCredential(pithosSettings.ProxyUsername, pithosSettings.ProxyPassword, pithosSettings.ProxyDomain);
37
                }
38
                //Otherwise, if there are generic authentication settings, use them
39
                if (!String.IsNullOrWhiteSpace(CredentialCache.DefaultNetworkCredentials.UserName))
40
                {
41
                    proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
42
                }
43
            }
44
            //In all other cases, use the default proxy
45
            else 
46
            {
47
                proxy = WebRequest.GetSystemWebProxy();
48
                //We can't use any previous proxy credentials when we pick the default proxy
49
                //If we set the proxy due to a change in settings, the credentials will only be 
50
                //valid for the previous proxy.
51
                proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
52
            }
53
            WebRequest.DefaultWebProxy = proxy;
54
        }
55

    
56

    
57
        public static void SetCredentials(string proxyUsername, string proxyPassword, string proxyDomain)
58
        {
59
            CredentialCache.DefaultNetworkCredentials.Password = proxyPassword;
60
            CredentialCache.DefaultNetworkCredentials.UserName = proxyUsername;
61
            CredentialCache.DefaultNetworkCredentials.Domain = proxyDomain;
62
            if (WebRequest.DefaultWebProxy!=null)
63
                WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
64
        }
65
    }
66
}