2016-03-21 36 views
1

私はいくつかの外部サービスを使用するASP.NET MVCアプリケーションを使用しています。プログラムでWCFサービスエンドポイントを変更した後のセキュリティ設定を維持する

私たちは、この方法で、web.configファイル内の各サービスにエンドポイントを定義します。

<endpoint address="http://tempuri.org/myExternalServiceEndPointAddress" 
    binding="basicHttpBinding" bindingConfiguration="myExternalServiceBinding" contract="myExternalServiceContract" name="MyExternalServiceName"> 
    <headers> 
     <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <wsse:UsernameToken> 
      <wsse:Username>myUsername</wsse:Username> 
      <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">myPassword</wsse:Password> 
     </wsse:UsernameToken> 
     </wsse:Security> 
    </headers> 
    </endpoint> 

本番環境への切り替え時に今、私たちのように、特定のサービスを通じて、実際のエンドポイントURLを発見しました彼らは警告なしに変更することができます。新しいエンドポイントが見つかったら、我々はこれで任意のメソッド呼び出しを実行する前に、エンドポイントURLを変更します。

myserviceClientObject.Endpoint.Address = new EndpointAddress(newEndpointUrl); 

我々はこれを行うと、メソッドの呼び出しが正しくnewEndpointUrlで取り組まれているが、コールはどこ検証ヘッダーノードを失いました含まれています。

我々は(我々はエンドポイントを変更するまで、前のものは完全に働いたが)しかし、サービスはこの認証を認識しているようだしない

myServiceClientObject.ClientCredentials.Username.Username = myUsername; 
myServiceClientObject.ClientCredentials.Username.Password = myPassword; 

を通じてプログラム的に認証を追加しようとしました。

洞察力や提案は非常に高く評価されます。

+0

エンドポイントが変更されるのはなぜですか?確かに別のサービスからの動的な応答、それの妥当性を保証できますか?あなたが持っている実際の問題に関しては、エンドポイントの設定に関連するwsse名前空間の参照は何ですか?私はちょうど "ClientCredentials"の部分を使用して十分ではないかもしれないと思う(あなたはセキュリティのパスワードの部分に特別なタイプがあります) –

答えて

0

私はついにこの問題を解決しました。

最後に、目的のヘッダーを持つオブジェクトをWCFパイプラインに配線しました。

これを行うには、サービス内でクライアントの動作を追加したクラスと、注入が行われた動作自体を定義したクラスを作成します。ここで

コードです:行動を追加

クラスを定義し、次に要求

public class SimpleMessageInspector : IClientMessageInspector, IDispatchMessageInspector { 

    public void AfterReceiveReply(ref Message reply, object correlationState) { 
    } 

    public object BeforeSendRequest(ref Message request, IClientChannel channel) { 


     UsernameToken authentication = new UsernameToken(remoteServiceUsername, remoteServicePassword, PasswordOption.SendPlainText); //Plain text is server requirement, we cannot do anything 

     var webUserHeader = MessageHeader.CreateHeader("Security", 
      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", authentication.GetXml(new XmlDocument())); 
     request.Headers.Add(webUserHeader); 

     return null; 
    } 

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { 
     return null; 
    } 

    public void BeforeSendReply(ref Message reply, object correlationState) { 
    } 
} 

を送信する前にそれを適用し

public class EndpointAddCredentials : IEndpointBehavior { 

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } 

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { 
     clientRuntime.MessageInspectors.Add(new SimpleMessageInspector()); 
    } 

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { } 

    public void Validate(ServiceEndpoint endpoint) { } 

} 

クラス、サービスのリダイレクトした後、私たちは持っていますクライアントオブジェクトにビヘイビアを追加するには

serviceObject.Endpoint.Behaviors.Add(new EndpointAddCredentials()); 

これで、サーバーへのリクエストには、私たちが探していたセキュリティ定義が含まれており、サーバーはそれを受け入れました。

関連する問題