我有一个由桌面应用程序托管的自托管 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!");
现在,代码成功完成。
我很难弄清楚如何删除此要求。我试过在客户端上搞乱绑定设置,但没有成功。
谁能指出我正确的方向?