C# .NET
Framework 4.0
I have an
application client that calls a WCF service and in this client uses a proxy for
the internet connection, so when I call my service I must first provide the
proxy authentication.
I have
created a class that implements the IWebProxy interface:
namespace Utililty {
public class ProxyConfig : IWebProxy {
public ICredentials Credentials {
get
{
using (var context = new AOEntities())
{
var query =
from data in context.ConnectionParameters
where data.ServerName.Equals("Proxy")
select data;
if (query.Any())
{
var parameters =
query.FirstOrDefault();
if (parameters != null) return new NetworkCredential(parameters.UserName,
parameters.Password);
}
return null;
}
}
set { }
}
public Uri GetProxy(Uri destination) {
return WebRequest.GetSystemWebProxy().GetProxy(destination);
}
public bool IsBypassed(Uri host) {
return WebRequest.GetSystemWebProxy().IsBypassed(host);
}
}
}
In this example,
I retrieve the credentials from a SQL Server database, but if you prefer you
can put in the application configuration file.
Then in the
application configuration file (app.config), I have added this section:
<system.net>
<defaultProxy>
<module type=" Utililty.ProxyConfig, Utililty"/>
</defaultProxy>
</system.net>
and the attribute useDefaultWebProxy="true" at the WCF binding configuration.
The ProxyConfig
object will have call automatically before your call to service.
Peace & Love!
Nessun commento:
Posta un commento