问题
In my desktop application (C#, WPF, WCF, .NET4.0) I addes a service reference.
This is Web Service (SOAP) using certificate and UserNameToken in Soap Envelope Header.
I add service reference (Solution -> Service Reference -> Add Service Reference) using WSDL of this web service. In my app.config I have got:
<customBinding>
<binding name="tmsIntegrationServiceSOAP">
<!-- WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'urn:CDM/tmsIntegrationService/': -->
<!-- <wsdl:binding name='tmsIntegrationServiceSOAP'> -->
<!-- <sp:SupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">..</sp:SupportingTokens> -->
<mtomMessageEncoding messageVersion="Soap11" />
<httpsTransport />
</binding>
</customBinding>
<endpoint address="https://XXX/CDM/tmsIntegrationService"
binding="customBinding" bindingConfiguration="tmsIntegrationServiceSOAP"
contract="RABEN.GS1.tmsIntegrationService" name="tmsIntegrationServiceSOAP" />
I'm passing user name and password like this:
var ssc = new GS1.tmsIntegrationServiceClient();
ssc.ClientCredentials.UserName.UserName = "test";
ssc.ClientCredentials.UserName.Password = "testPassword";
ssc.Endpoint.Behaviors.Add(new InspectorBehavior());
When I get the request XML (with message inspector) I see that there is no UserNameToken in soap header
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
<VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPoy3hcoBMqQ5Kme7yqEiHKs0AAAAAyxy+QnWD8U60kqJZWaGfvYD8RN14nUVIjC0RuEyVBa8ACQAA</VsDebuggerCausalityData>
</s:Header>
<s:Body ...
I try to user basicHttpBinding:
<binding name="secured">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
<endpoint address="https://XXX/CDM/tmsIntegrationService"
binding="basicHttpBinding" bindingConfiguration="secured" contract="RABEN.GS1.tmsIntegrationService"
name="tmsIntegrationServiceSOAP" />
End the result:
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
</s:Header>
When i try to use wsHttpBinding:
<wsHttpBinding>
<binding name="RabenBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
<endpoint address="https://XXX/CDM/tmsIntegrationService"
binding="wsHttpBinding" bindingConfiguration="RabenBinding"
contract="RABEN.GS1.tmsIntegrationService" name="tmsIntegrationServiceSOAP" />
The result:
<s:Header>
<a:Action s:mustUnderstand="1">urn:CDM/tmsIntegrationService/importTransportInstruction</a:Action>
<a:MessageID>urn:uuid:701a0fff-c4aa-4f37-a299-ec6d272e51e7</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</s:Header>
Could some one tell me what I'm doing wrong?
EDIT - 2017-06-21 20:00 The certificate is installed on my local machine
<behaviors>
<endpointBehaviors>
<behavior name="RabenBehavior">
<clientCredentials>
<clientCertificate findValue="this is footprint of certificate"
storeLocation="CurrentUser" storeName="My" x509FindType="FindByThumbprint" />
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
I'm attaching this behavion in endpoint:
<endpoint address="https://XXX/CDM/tmsIntegrationService"
behaviorConfiguration="RabenBehavior" binding="wsHttpBinding"
bindingConfiguration="RabenBinding" contract="RABEN.GS1.tmsIntegrationService"
name="tmsIntegrationServiceSOAP" />
EDIT 2017-06-21 20:58
I try to use <mtomMessageEncoding messageVersion="Soap11" />
<binding name="myCustomBindingConfig">
<security defaultAlgorithmSuite="Default"
authenticationMode="UserNameOverTransport"
requireDerivedKeys="true"
includeTimestamp="false" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" />
<mtomMessageEncoding messageVersion="Soap11" />
<httpsTransport maxReceivedMessageSize="2000000000" />
</binding>
But the header is still do not containt user name token
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
<VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo0OlG2DVOLdKniJmYU2kvwUAAAAAujMM+x53aEWJYYd4GKyk+PlCKXIih9xLrE0V5TayKhQACQAA</VsDebuggerCausalityData>
</s:Header>
EDIT 2017-06-21 21:36
Try to use <textMessageEncoding messageVersion="Soap11" />
in custom binding generatet by VS
<binding name="tmsIntegrationServiceSOAP">
<!-- WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'urn:CDM/tmsIntegrationService/': -->
<!-- <wsdl:binding name='tmsIntegrationServiceSOAP'> -->
<!-- <sp:SupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">..</sp:SupportingTokens> -->
<textMessageEncoding messageVersion="Soap11" />
<httpsTransport />
</binding>
But it bring no effect - there is no user name token in request XML:
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
<VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo0wJNixU4QRBhGEUAC3pw50AAAAA/fPJ+e50KUSGbYXwcmoGwgqymCvLXJZIhKg/nKdV97cACQAA</VsDebuggerCausalityData>
</s:Header>
回答1:
but what happend if you add by click on the solution explorer add service reference, what it is telling you?. Or you don't have this option.
回答2:
To modify SOAP Header you will need to implement IClientMessageInspector interface.To do this create new class MessageInspector and add three following classes nested in it:
public class CustomMessageHeader : MessageHeader
{
private const string NAMESPACE_SECURITY = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
public CustomMessageHeader()
{
}
public override string Name
{
get { return "wsse:Security"; }
}
public override string Namespace
{
get { return ""; }
}
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
writer.WriteAttributeString("xmlns", "wsse", null, NAMESPACE_SECURITY);
writer.WriteStartElement("wsse:UsernameToken");
writer.WriteElementString("wsse:Username", "YOUR_USERNAME");
writer.WriteElementString("wsse:Password", "YOUR_PASSWORD");
writer.WriteEndElement();
}
}
public class ClientMessageInspector : IClientMessageInspector
{
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
CustomMessageHeader header = new CustomMessageHeader();
request.Headers.RemoveAt(0);
request.Headers.Add(header);
return request;
}
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
}
}
public class CustomEndpointBehavior : IEndpointBehavior
{
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
To trigger this class to be called, add new Endpoint Behavior in your Main class
serviceClient.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior());
来源:https://stackoverflow.com/questions/44682532/c-sharp-wcf-service-reference-can-not-create-usernametoken