我有一个由桌面应用程序托管的自托管WCF服务 我可以在我的PC上本地连接到本地服务,但我无法远程使用该服务,至少在没有提供我的Windows /域级别凭证的情况下。
我使用以下代码在应用中启动服务:
ServiceHost host = new ServiceHost(
typeof (SMService),
new Uri("net.tcp://localhost:" + SMGlobals._DEFAULTSERVICEPORT.ToString() + "/SMService"));
host.AddServiceEndpoint(
typeof(ISMService),
new NetTcpBinding(),
"");
System.ServiceModel.Channels.Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
var metadataBehavior =
new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
host.AddServiceEndpoint(
typeof(IMetadataExchange),
mexBinding,
"net.tcp://localhost:" + SMGlobals._DEFAULTSERVICEPORT.ToString() + "/SMService/mex");
host.Open();
SMGlobals.SMServiceHost = host;
如果我使用以下代码创建一个简单的客户端来调用服务:
var client = new SMServiceClient();
var uri = "net.tcp://192.168.11.10:8760/SMService";
client.Endpoint.Address = new EndpointAddress(uri);
var initiateResponse = client.InitiateAuthentication(new InitiateAuthenticationRequest());
MessageBox.Show("Success!");
我收到以下例外:
System.ServiceModel.Security.SecurityNegotiationException:服务器已拒绝客户端凭据。 ---> System.Security.Authentication.InvalidCredentialException:服务器已拒绝客户端凭据。 ---> System.ComponentModel.Win32Exception:登录尝试失败
现在,从其他研究中,我发现我可以使用以下代码在客户端调用中提供我的凭据:
var client = new SMServiceClient();
var uri = "net.tcp://192.168.11.10:8760/SMService";
client.Endpoint.Address = new EndpointAddress(uri);
client.ClientCredentials.Windows.ClientCredential.Domain = "domain";
client.ClientCredentials.Windows.ClientCredential.UserName = "my_user_name";
client.ClientCredentials.Windows.ClientCredential.Password = "my_password";
var initiateResponse = client.InitiateAuthentication(new InitiateAuthenticationRequest());
MessageBox.Show("Success!");
现在,代码已成功完成。
我很难弄清楚如何删除此要求。我已经尝试在客户端上搞乱绑定设置而没有成功。
有人能指出我正确的方向吗?
答案 0 :(得分:1)
默认情况下,Tcp Binding已启用安全性,因此要获得所需内容,您需要明确将其关闭。像这样添加你的端点。您也可以浏览NetTcpBinding的MSDN帮助,因为您可能希望使用备用构造函数来关闭可靠的消息传递。
host.AddServiceEndpoint(
typeof(ISMService),
new NetTcpBinding(SecurityMode.None),
"");
答案 1 :(得分:0)
Set the appropriate Authentication on the Binding.
ClientAuthenticationType = “无”
答案 2 :(得分:0)
我有类似的问题。在两个进程之间本地工作,但是当两个进程放在不同的机器上时(或在本地使用解析为本地机器的公共URL,例如mylocalmachine.corp.com),相同的代码失败。我发现我需要将匿名绑定的安全性明确设置为“无”:
<binding name="TcpAnonymousBinding" portSharingEnabled="true" receiveTimeout="24:00:00">
<security mode="None"/>
</binding>