デスクトップ アプリケーションによってホストされるセルフホステッド 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!");
これで、コードは正常に完了しました。
この要件を削除する方法を理解するのに苦労しています。クライアントのバインド設定をいじってみましたが、成功しませんでした。
誰かが私を正しい方向に向けることができますか?