簡體   English   中英

如何限制特定IP的WCF服務方法

[英]How to limit wcf service method for a specific ip

這是我的wcf服務方法:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/CheckID/{id}")]
    public string CheckID(string id)
    {
             /*Check reuqest where it comes from */
    }

我希望我的方法可以從http://particularIP.com調用響應來發送響應,除非響應是錯誤請求。

我怎樣才能做到這一點?

您可以在web.config文件中使用IP過濾器,例如:

<serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="RestrictedServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <IPFilter filter="172.*.*.* 127.0.0.1" />          
    </behavior>
  </serviceBehaviors>

已編輯

或者可以使用ServiceAuthorizationManager.CheckAccessCore,在其中您可以從OperationContext獲取客戶端IP。

https://msdn.microsoft.com/zh-CN/library/system.servicemodel.serviceauthorizationmanager.checkaccesscore.aspx

編輯2

using System.ServiceModel;
using System.ServiceModel.Channels;

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
    prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM