1

デスクトップ アプリケーションによってホストされるセルフホステッド 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!");

これで、コードは正常に完了しました。

この要件を削除する方法を理解するのに苦労しています。クライアントのバインド設定をいじってみましたが、成功しませんでした。

誰かが私を正しい方向に向けることができますか?

4

3 に答える 3

1

Tcp バインディングではデフォルトでセキュリティが有効になっているため、必要なものを取得するには、明示的に無効にする必要があります。このようにエンドポイントを追加します。また、別のコンストラクターを使用して信頼できるメッセージングをオフにすることもできるため、NetTcpBinding の MSDN ヘルプを調べることもできます。

 host.AddServiceEndpoint(
            typeof(ISMService),
            new NetTcpBinding(SecurityMode.None),
            "");
于 2011-02-22T23:15:56.800 に答える
0

同様の問題がありました。2つのプロセス間でローカルに機能しましたが、2つのプロセスが異なるマシンに配置された場合(またはローカルマシンに解決されたパブリックURLをローカルで使用した場合(mylocalmachine.corp.comなど)に同じコードが失敗しました)。匿名バインディングのセキュリティを「なし」に明示的に設定する必要があることがわかりました。

<binding name="TcpAnonymousBinding" portSharingEnabled="true" receiveTimeout="24:00:00">
    <security mode="None"/>
</binding>
于 2012-11-17T15:57:35.910 に答える
0

バインディングに適切な認証を設定します。

ClientAuthenticationType="なし"

于 2011-02-22T23:04:44.700 に答える