wcf icon indicating copy to clipboard operation
wcf copied to clipboard

Support security mode TransportWithMessageCredential

Open iamjasonp opened this issue 11 years ago • 71 comments

Message level security is not currently supported. Attempting to use it will throw PlatformNotSupportedException

iamjasonp avatar May 13 '15 17:05 iamjasonp

Currently cannot move our Windows 8.1 store application to Windows 10 because this is not implemented.

fdsprod avatar Dec 15 '15 19:12 fdsprod

Is there any progress related with this topic, Please i need it to work in it.

oscar55 avatar Dec 25 '15 16:12 oscar55

Just got burned by this one too.

ctolkien avatar Jun 18 '16 08:06 ctolkien

getting burned by lack of wsHttpBinding here now as well.

bc3tech avatar Jul 20 '16 14:07 bc3tech

This is currently still blocked by the lack of message security feature.

zhenlan avatar Nov 29 '16 22:11 zhenlan

I need this too!

havheg avatar Feb 23 '17 09:02 havheg

~~This has become more pressing for us with the announcement to ASP.NET Core 2 will not target NetStandard2. We need this in to move forward!~~

They are still going to target NetStandard2, this is however what is holding us to full framework.

ctolkien avatar May 08 '17 05:05 ctolkien

Any news wether this is on the short term roadmap?

Oosterbosch avatar Aug 08 '17 10:08 Oosterbosch

Any update on the status of fixing this?

uwbadgerdev avatar Dec 07 '17 20:12 uwbadgerdev

This has been an issue now for 2 and a half years, why is this basic functionality still not supported?

nickcoad avatar Jan 16 '18 02:01 nickcoad

I ended up sidestepping WCF altogether to solve this: https://stackoverflow.com/a/48818293

kevin-osborne avatar Feb 18 '18 13:02 kevin-osborne

I need this support!

edenprairie avatar Jul 11 '18 03:07 edenprairie

Any official workaround, or otherwise timeline for a patch? Thanks! @Lxiamail

OpenSpacesAndPlaces avatar Sep 06 '18 03:09 OpenSpacesAndPlaces

Normal Way

client = new MyClient(new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential), new EndpointAddress(BaseUrl));
client.ClientCredentials.UserName.UserName = UserName;
client.ClientCredentials.UserName.Password = Password;

Workaround

client = new MyClient(new BasicHttpBinding(BasicHttpSecurityMode.Transport), new EndpointAddress(BaseUrl));
client.Endpoint.SetMessageCredentials(UserName,Password);

Generates a header like:

 <soap:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken>
        <wsse:Username>user</wsse:Username>
        <wsse:Password>password</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soap:Header>

Credit for solution basis: https://stackoverflow.com/questions/734355/clueless-about-how-to-create-soap-wssesecurity-header http://www.ideliverable.com/blog/calling-salesforce-soap-apis-from-a-net-core-or-uwp-app

	internal class AuthenticationHeader : MessageHeader
	{
		public AuthenticationHeader(string UserName, string Password,bool IncludeTimestamp)
		{
			_UserName = UserName;
			_Password = Password;
			_IncludeTimestamp = IncludeTimestamp;
		}

		private string _UserName;
		private string _Password;
		private bool _IncludeTimestamp;

		public override string Name => "Security";

		public override string Namespace => "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

		protected override void OnWriteStartHeader(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion messageVersion)
		{
			writer.WriteStartElement("wsse", Name, Namespace);
			writer.WriteXmlnsAttribute("wsse", Namespace);
		}

		protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
		{
			//TODO Timestamp - If Needed

			writer.WriteStartElement("wsse", "UsernameToken", Namespace);

			writer.WriteStartElement("wsse", "Username", Namespace);
			writer.WriteValue(_UserName);
			writer.WriteEndElement();

			writer.WriteStartElement("wsse", "Password", Namespace);
			writer.WriteValue(_Password);
			writer.WriteEndElement();

			writer.WriteEndElement();
		}
	}
internal class AuthenticationHeaderBehavior : IEndpointBehavior
	{
		public AuthenticationHeaderBehavior(string UserName, string Password, bool IncludeTimestamp)
		{
			_UserName = UserName;
			_Password = Password;
			_IncludeTimestamp = IncludeTimestamp;
		}

		private string _UserName;
		private string _Password;
		private bool _IncludeTimestamp;

		public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
		{

		}

		public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
		{
			clientRuntime.ClientMessageInspectors.Add(new AuthenticationHeaderInspector(_UserName, _Password, _IncludeTimestamp));
		}

		public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
		{

		}

		public void Validate(ServiceEndpoint endpoint)
		{

		}
	}
	internal static class AuthenticationHeaderExtensions
	{
		public static void SetMessageCredentials(this ServiceEndpoint endpoint, string UserName, string Password)//, bool IncludeTimestamp)
		{
			//TODO Timestamp - If Needed
			endpoint.EndpointBehaviors.Add(new AuthenticationHeaderBehavior(UserName, Password, false));//IncludeTimestamp));
		}
	}
	internal class AuthenticationHeaderInspector : IClientMessageInspector
	{
		public AuthenticationHeaderInspector(string UserName, string Password, bool IncludeTimestamp)
		{
			_UserName = UserName;
			_Password = Password;
			_IncludeTimestamp = IncludeTimestamp;
		}

		private string _UserName;
		private string _Password;
		private bool _IncludeTimestamp;

		public void AfterReceiveReply(ref Message reply, object correlationState)
		{

		}

		public object BeforeSendRequest(ref Message request, IClientChannel channel)
		{
			request.Headers.Add(new AuthenticationHeader(_UserName, _Password, _IncludeTimestamp));

			return Guid.NewGuid();
		}
	}

OpenSpacesAndPlaces avatar Sep 06 '18 14:09 OpenSpacesAndPlaces

Hi @OpenSpacesAndPlaces, I try your solution with custom message Header but my result is always :

MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic Realm = 'Integration Server''.

:( I'm in .NET Core 2.1 with BasicHttpBinding and TransportCredentialsOnly Thanks you for your help

Jhatzig avatar Sep 06 '18 15:09 Jhatzig

@Jhatzig

I'm not sure on your specific case - but I'd confirm you have the correct "BasicHttpSecurityMode" set. In my build out it went from: BasicHttpSecurityMode.TransportWithMessageCredential to: BasicHttpSecurityMode.Transport

Also keep in mind that not all SOAP authentication security headers are the same. You may need to customize the XML generated by: AuthenticationHeader : MessageHeader

The output this generates is like:

  <soap:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken>
        <wsse:Username>user</wsse:Username>
        <wsse:Password>password</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soap:Header>

OpenSpacesAndPlaces avatar Sep 06 '18 18:09 OpenSpacesAndPlaces

@OpenSpacesAndPlaces

Thanks your for your answer. My case is : A WS with WCF in HTTP with TransportCredentialOnly With .NET classic, in web.config , on binding, we add the message security : <message clientCredentialType="UserName" algorithmSuite="Default" />

but with .Net Core, we can't do that. So, I try to use your solution but it's for TransportWithMessageCredential and i'm not founding the solution for TransportCredentialOnly*

If, i'm setting to "Transport" i have the following error : ArgumentException: The provided URI scheme 'http' is invalid; expected 'https'. because my WS is in HTTP

Jhatzig avatar Sep 07 '18 07:09 Jhatzig

@Jhatzig

BasicHttpSecurityMode.Transport Or similar, is going to require an "Https" url.

You may be looking for (but I'd caution using that in anything but a development environment): BasicHttpSecurityMode.None

As far as using the equivalent of: TransportCredentialOnly

To my understanding, you'd need HTTP headers instead of a SOAP header - so something like this (untested) - is probably what you're after. That's kind of the direction you'll need to dig is how to add HTTP headers to a WCF requests manually.

BasicHttpSecurityMode.None

	internal class AuthenticationHeaderInspector : IClientMessageInspector
	{
		public AuthenticationHeaderInspector(string UserName, string Password, bool IncludeTimestamp)
		{
			_UserName = UserName;
			_Password = Password;
			_IncludeTimestamp = IncludeTimestamp;
		}

		private string _UserName;
		private string _Password;
		private bool _IncludeTimestamp;

		public void AfterReceiveReply(ref Message reply, object correlationState)
		{

		}

		public object BeforeSendRequest(ref Message request, IClientChannel channel)
		{
			using (new System.ServiceModel.OperationContextScope(channel))
			{
				MessageHeader head = MessageHeader.CreateHeader("Authorization", "http://yournamespace.com/v1", "Basic yourcredentials");
				OperationContext.Current.OutgoingMessageHeaders.Add(head);
			}

			return Guid.NewGuid();
		}
}

Header portion sourced from: https://stackoverflow.com/questions/14621544/how-can-i-add-authorization-header-to-the-request-in-wcf

OpenSpacesAndPlaces avatar Sep 07 '18 12:09 OpenSpacesAndPlaces

While this works, be aware that it doesn't support WS-SecureConversation which is needed for default support for WSHttpBinding. The difference when using WS-SecureConversation is a handshake happens at channel open where a secure token is negotiated and used in actual payload messages being sent back and forth. This has two advantages. First, it allows a persistent session on the server side so you can have a persistent instance context/service instance for a single client channel. Second, with more complicated tokens than username and password, the token validation can be a bit more expensive. Sending the full authentication token every time requires it to be re-validated for every request. The problem we have with in-box support is WCF depends on System.IdentityModel which the owning team don't wish to port to .NET Core. So far I've prototyped on the .NET Framework removing most of the crypto dependency of IdentityModel and I was able to get this scenario working with minimal dependency on IdentityModel (using the SignedXml implementation that's available in .NET Core instead). The next step is to bring over the WCF pieces and my prototype to .NET Core along with some pieces of IdentityModel which we will now need to own.
So in short, there are some difficult blockers but I'm working on it and have a path to get this working with a successful proof of concept prototype.

mconnew avatar Sep 10 '18 18:09 mconnew

It's been three and a half years and this issue still hasn't been resolved? I too am being bit by this issue. I will try the work-arounds suggested above but it's kind of ridiculous that this has not been fixed yet.

elmojo avatar Sep 17 '18 17:09 elmojo