Hi,
Im trying to make a call to a Webservice using SOP. The endpoint requires authentication with a certificate (pfx file).
I added the certificate to the code but it returns a error saying that 'anonymous client is not allowed'. Can anyone help me, please?
var cert = new X509Certificate2("myCert.pfx", "myPassword");
var binding = new BasicHtt psBinding();
binding.Security.Mode = BasicHtt psSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = Htt pClientCredentialType.Certificate;
// Specify the endpoint address
var endpoint = new EndpointAddress("the end point"); // I cannot post the link. The website wouldnt let me.
var client = new NFeDistribuicaoDFeSoapClient(NFeDistribuicaoDFeSoapClient.EndpointConfiguration.NFeDistribuicaoDFeSoap, endpoint);
client.ClientCredentials.ClientCertificate.Certificate = cert;
var nfeDadosMsgXml = @"
2
35
000000000000000
0
";
XmlDocument doc = new XmlDocument();
doc.LoadXml(nfeDadosMsgXml);
var requestBody = new nfeDistDFeInteresseRequestBody { nfeDadosMsg = doc.DocumentElement };
var request = new nfeDistDFeInteresseRequest(requestBody);
try
{
var response = await client.nfeDistDFeInteresseAsync(doc.DocumentElement);
Console.WriteLine(response.Body.nfeDistDFeInteresseResult.OuterXml);
}
catch (Exception ex)
{
Console.WriteLine($"Error calling service: {ex.Message}");
}
Prasad RaveendranPosted Jun 25, 2024, 11:16 PM
enable the WCF tracing and see what is the exact issue
Andre SilveiraPosted Jun 24, 2024, 11:42 PM
Your code is pretty much like mine,
The erro persists. I inspected the request and the certificate is there... this is not it.
Prasad RaveendranPosted Jun 24, 2024, 10:43 PM
The error message "anonymous client is not allowed" suggests that the client certificate is not being sent or recognized properly by the server. Here are a few steps to ensure that the client certificate is correctly set up and included in the SOAP request:
Load and Attach the Certificate Correctly: Ensure that the certificate is correctly loaded and attached to the client credentials.
Check the Binding Configuration: Make sure that the
BasicHttpsBindingis configured properly to use the certificate for client authentication.Verify the Endpoint Address: Double-check the endpoint address to ensure it is correct and properly secured with HTTPS.
Here's an updated version of your code with some additional checks and configurations:
Key Points:
BasicHttpsBindingis used for secure communication, and setClientCredentialTypetoCertificate.https.Troubleshooting Tips:
If the issue persists, you might need to enable WCF tracing to get more detailed error messages, which can help diagnose what might be going wrong.