Consume WSDL With Message Certificates .Net Core 5

Rizwan Ansari 91 Reputation points
2021-11-02T11:18:34.527+00:00

Dear Experts,

Some service provider gave physical wsdl and certificates, also they provided sample in .net framework 4.7.
In .net framework the below code works

       var binding = new BasicHttpsBinding();
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        var dns = new DnsEndpointIdentity(ConfigurationManager.AppSettings["DnsEndpointIdentity"]);
        var address = new EndpointAddress(new Uri(ConfigurationManager.AppSettings["EndpointAddress"]), dns, new AddressHeader[] { });
        binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
        var securityElement = SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10, true);
        securityElement.IncludeTimestamp = true;
        securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
        var customBinding = new CustomBinding(binding);
        customBinding.Elements.Insert(0, securityElement);

        Client proxy = new Client(customBinding, address);
        proxy.ChannelFactory.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.Sign;
        proxy.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings["ClientCertificate"]);
        proxy.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust;
        proxy.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings["ServiceCertificate"]);
        proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 0, 3, 0);
        return proxy;

Same code not working in .net core 5, please help.

Regards

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.