Skip to content

Instantly share code, notes, and snippets.

@BrightSoul
Created May 13, 2018 12:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrightSoul/a97741be6844b76019a41991e7175e2c to your computer and use it in GitHub Desktop.
Save BrightSoul/a97741be6844b76019a41991e7175e2c to your computer and use it in GitHub Desktop.
Invocazione webservice sogei
static void Main(string[] args)
{
var address = new EndpointAddress(new Uri("https://url-del-servizio-sogei"));
var binding = CreateMultiFactorAuthenticationBinding();
ServicePointManager.ServerCertificateValidationCallback = ValidateCallback;
var factory = new ChannelFactory<NomeInterfacciaProxyClient>(binding, address);
var certificate = new X509Certificate2(@"percorso.pfx", "password");
factory.Credentials.ClientCertificate.Certificate = certificate;
factory.Endpoint.Contract.ProtectionLevel = ProtectionLevel.Sign;
factory.Credentials.ServiceCertificate.DefaultCertificate = new X509Certificate2(@"certificato-del-server.crt");
factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
{
CertificateValidationMode = X509CertificateValidationMode.None,
RevocationMode = X509RevocationMode.NoCheck
};
var channel = factory.CreateChannel();
try
{
var request = new OggettoRichiesta
{
//Omissis
};
var response = await channel.nomeOperazione(request);
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
Console.ReadLine();
}
private static bool ValidateCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public static Binding CreateMultiFactorAuthenticationBinding()
{
System.ServiceModel.Channels.AsymmetricSecurityBindingElement asbe = new AsymmetricSecurityBindingElement();
asbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;
asbe.InitiatorTokenParameters = new X509SecurityTokenParameters();
asbe.RecipientTokenParameters = new X509SecurityTokenParameters();
asbe.ProtectTokens = true;
asbe.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
asbe.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
asbe.EnableUnsecuredResponse = true;
asbe.IncludeTimestamp = false;
asbe.SetKeyDerivation(false);
asbe.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic128Rsa15;
CustomBinding myBinding = new CustomBinding();
myBinding.Elements.Add(asbe);
myBinding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8));
HttpsTransportBindingElement httpsBindingElement = new HttpsTransportBindingElement();
httpsBindingElement.RequireClientCertificate = true;
myBinding.Elements.Add(httpsBindingElement);
return myBinding;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment