0

いくつかの単純な関数を使用して、Restful WCF サービスを構築しました。新しい要件が提起されました。

関数の 1 つは、特定の IP 範囲からのみアクセスできるようにする必要があります。

これを実装する最良の方法は何ですか?簡単な方法は、リクエストパターンに従ってIP範囲をブロックするルールでIISを構成することだと思いました-そのようなオプションが見つかりません..

ありがとう!提供する

4

2 に答える 2

1

実装してみましたIParameterInspectorか?コードは次のようになります。

public class IPFilterAttribute : Attribute, IOperationBehavior, IParameterInspector
{
    private string _rangeFrom;
    private string _rangeTo;

    public IPFilterAttribute(string rangeFrom, string rangeTo)
    {
        _rangeFrom = rangeFrom;
        _rangeTo = rangeTo;
    }

    public void ApplyDispatchBehavior(
        OperationDescription operationDescription,
        DispatchOperation dispatchOperation)
    {
        dispatchOperation.ParameterInspectors.Add(this);
    }

    public void AfterCall(string operationName, object[] outputs,
                          object returnValue, object correlationState)
    {
    }

    public object BeforeCall(string operationName, object[] inputs)
    {
        RemoteEndpointMessageProperty clientEndpoint =
            OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
        if (!IsClientInInRange(clientEndpoint.Address))
        {
            throw new SecurityException(string.Format("Calling method '{0}' is not allowed from address '{1}'.", operationName, clientEndpoint.Address));
        }

        return null;
    }

    private bool IsClientInRange(string clientAddress)
    {
        // do the magic to check if client address is in the givn range
    }

    public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
    }

    public void Validate(OperationDescription operationDescription)
    {
    }
}

次に、この属性で Web メソッドを装飾するだけです。

    [OperationContract]
    [WebInvoke(...)]
    [IPFilter("64.18.0.0", "64.18.15.255")]
    string GetData(string value);
于 2012-11-07T23:21:53.330 に答える
0

いくつかのオプション:-ファイアウォールを使用してこの仕事をすることができます

  • IISにはIPをブロックできる機能がありますが、IISでサービスをホストする必要があります。

  • WCFを使用してクライアントアドレスを取得し、呼び出しを受け入れる/拒否することができます。

参照: http ://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/

于 2012-11-07T10:37:36.747 に答える