I am trying to create a WCF Service (hosted in a windows service) using message security mode with a custom username validator.
I have also created a self-signed certificate through IIS.
The problem is that i encounter the following error:
System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.
WCF Server Configuration:
// Create a WsHttpBinding.
// Set Message Security Mode and UserName Authorization
WSHttpBinding mywshttpbind = new WSHttpBinding();
mywshttpbind.Security.Mode = SecurityMode.Message;
mywshttpbind.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
// Negotiate the Credentials and establish a Security Token
mywshttpbind.Security.Message.NegotiateServiceCredential = true;
mywshttpbind.Security.Message.EstablishSecurityContext = false;
// Find the BindingElements and disable the TimeStamp
BindingElementCollection elements = mywshttpbind.CreateBindingElements();
elements.Find<SecurityBindingElement>().IncludeTimestamp = false;
CustomBinding myCustomBinding = new CustomBinding(mywshttpbind);
// A ServiceCertificate will be used - search it by serial number
obj.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySerialNumber, "....");
// The client will not check for the certificate
obj.Credentials.ClientCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
// A custom UsernamePassword Validator will be used
obj.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
obj.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserPassValidator();
// Add Endpoint to Host with the custom binding
obj.AddServiceEndpoint(typeof(service.IService), myCustomBinding, "");
// Metadata Exchange
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
obj.Description.Behaviors.Add(serviceBehavior);
// Open the connection
obj.Open();
WCF Client:
// Create a WsHttpBinding.
// Set Message Security Mode and UserName Authorization
WSHttpBinding mywshttpbind = new WSHttpBinding();
mywshttpbind.Security.Mode = SecurityMode.Message;
mywshttpbind.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
// Negotiate the Credentials and establish a Security Token
mywshttpbind.Security.Message.NegotiateServiceCredential = true;
mywshttpbind.Security.Message.EstablishSecurityContext = false;
// Find the BindingElements and disable the TimeStamp
BindingElementCollection elements = mywshttpbind.CreateBindingElements();
elements.Find<SecurityBindingElement>().IncludeTimestamp = false;
CustomBinding myCustomBinding = new CustomBinding(mywshttpbind);
// Create a new factory channel
var newFactory = new ChannelFactory<ICMM_WCF_S>(myCustomBinding, add);
// Set the username and the password
newFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
newFactory.Credentials.UserName.UserName = "user1";
newFactory.Credentials.UserName.Password = "pass1";
var channel = newFactory.CreateChannel();
channel.get_data();
After i enabled the WCF Tracer i traced the following exception:
The security timestamp is invalid because its creation time ('2015-07-06T10:28:39.264Z') is in the future. Current time is '2015-07-06T10:20:44.570Z' and allowed clock skew is '00:05:00'.
Even i created a Custom Binding with the timestamp disabled the error persists.