[英]How to limit wcf service method for a specific ip
Here is my wcf service method: 这是我的wcf服务方法:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/CheckID/{id}")]
public string CheckID(string id)
{
/*Check reuqest where it comes from */
}
I want my method send response OK if it comes/is invoked from http://particularIP.com , unless response Bad request. 我希望我的方法可以从http://particularIP.com调用响应来发送响应,除非响应是错误请求。
How can i do that? 我怎样才能做到这一点?
You can use IP Filter in web.config file, like :- 您可以在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>
Edited 已编辑
Or use can ServiceAuthorizationManager.CheckAccessCore in which you get client IP from OperationContext. 或者可以使用ServiceAuthorizationManager.CheckAccessCore,在其中您可以从OperationContext获取客户端IP。
https://msdn.microsoft.com/en-us/library/system.servicemodel.serviceauthorizationmanager.checkaccesscore.aspx https://msdn.microsoft.com/zh-CN/library/system.servicemodel.serviceauthorizationmanager.checkaccesscore.aspx
Edit 2 编辑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.