I'm developing a client to use a web service. The endpoint is HTTPS. I get this exception when I try to log in. Why?
This is the method
BasicHttpsBinding binding = new BasicHttpsBinding();
binding.Security.Mode = BasicHttpsSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
var ea = new EndpointAddress(new Uri($@"https://endpoint"));
WSPDDClient client = new WSPDDClient(binding, ea);
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "certificatename");
WSPDD.login login1 = new WSPDD.login()
{
login1="username",
password="password"
};
try
{
client.Open();
WSPDD.loginResponse resLogin = client.login(login1);
if ([email protected])
{
}
else
{
Debug.WriteLine("Err {0}", [email protected]);
}
}
catch(Exception ex)
{
Debug.WriteLine("Err {0}", ex);
}
This is web.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WSPDDBinding" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://endpoint"
binding="basicHttpBinding" bindingConfiguration="WSPDDBinding"
contract="WSPDD" name="WSPDDPort" />
</client>
the exception is:
The HTTP request is not authorized with the 'Anonymous' client authentication scheme. Authentication header received from server: 'Mutual SSL realm = \ "WSO2 API Manager \", error = \ "invalid token \", error_description = \ "The access token expired \"'. "
The exception is on
WSPDD.loginResponse resLogin = client.login(login1);
All these code snippets are located on the client-side, and certain settings are duplicate, such as the Basichttpbinding configuration. The configuration settings in the code snippets are not in accord with that in the
Webconfig.The common way to call the WCF service is generating a client proxy by Adding service reference, which also brings binding settings that are consistent with the server-side in the configuration file located on the client-side.
From the errors occurred in the client-side, the binding configuration in the code snippets should be right, and correspond with the server-side.
In other words, the server authenticates the client with a certificate, the client should provide a client certificate when calling the remote service. during this process, we should establish the trust relationship between the server-side and the client-side.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication
Feel free to let me know if there is anything I can help with.