WCF With NetTCP Through Machines on the Same Network

I am trying to implement some interprocess communication that is between several computers and one server on the same network. Now I am trying to use WCF with NetTcpBinding located in my application that runs on one computer, but when I try to connect from another computer, it causes an SSPI security error.

I found many examples of this cross-machine, but they all include an app.config file that I REALLY want to avoid. I want to be able to embed this functionality in a DLL that has no other dependencies (for example, configuration files), for which I can just pass all the necessary server addresses to it, etc., and it will work. Is there a way to configure this security (through endpoints, etc.) exclusively in code?

I am testing all of this with the code below:

SERVER:

using System; using System.ServiceModel; namespace WCFServer { [ServiceContract] public interface IStringReverser { [OperationContract] string ReverseString(string value); } public class StringReverser : IStringReverser { public string ReverseString(string value) { char[] retVal = value.ToCharArray(); int idx = 0; for (int i = value.Length - 1; i >= 0; i--) retVal[idx++] = value[i]; string result = new string(retVal); Console.WriteLine(value + " -> " + result); return result; } } class Program { static void Main(string[] args) { var uri = "net.tcp://" + System.Net.Dns.GetHostName() + ":9985"; Console.WriteLine("Opening connection on: " + uri); using (ServiceHost host = new ServiceHost( typeof(StringReverser), new Uri[]{ new Uri("net.tcp://" + System.Net.Dns.GetHostName() + ":9985") })) { host.AddServiceEndpoint(typeof(IStringReverser), new NetTcpBinding(), "TcpReverse"); host.Open(); Console.WriteLine("Service is available. " + "Press <ENTER> to exit."); Console.ReadLine(); host.Close(); } } } } 

CLIENT:

 using System; using System.ServiceModel; using System.ServiceModel.Channels; namespace WCFClient { [ServiceContract] public interface IStringReverser { [OperationContract] string ReverseString(string value); } class Program { static void Main(string[] args) { var ep = "net.tcp://SERVER:9985/TcpReverse"; ChannelFactory<IStringReverser> pipeFactory = new ChannelFactory<IStringReverser>( new NetTcpBinding(), new EndpointAddress( ep)); IStringReverser pipeProxy = pipeFactory.CreateChannel(); Console.WriteLine("Connected to: " + ep); while (true) { string str = Console.ReadLine(); Console.WriteLine("pipe: " + pipeProxy.ReverseString(str)); } } } } 
+6
source share
2 answers

Security is usually configured to bind. You are using NetTcpBinding with default values, which means Transport protection is enabled.

On both servers and clients, you must assign an instance of NetTcpBinding local variable so that you can change the security settings (and possibly others) and then use this variable when calling AddServiceEndpoint or when creating a ChannelFactory .

Example:

 var binding = new NetTcpBinding(); // disable security: binding.Security.Mode = SecurityMode.None; 
+5
source

This is probably a problem with the SPN your service is running with. This is most likely a machine account, not a domain account. There is more information in this thread .

UPDATE: there is information on setting up SPN programmatically, but it looks like a few clicks in ... here is a direct link (see the last section of the page).

+1
source

Source: https://habr.com/ru/post/894059/


All Articles