2013-04-05 125 views
2

我们正在使用自定义UserNamePasswordValidator。这使得消费者能够如下指定SOAP头的凭据:确定WCF SOAP中客户端的IP地址UserNamePasswordValidator

<o:Security xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1"> 
<Timestamp Id="_0"> 
    <Created> 
     2013-04-05T16:35:07.341Z</Created> 
     <Expires>2013-04-05T16:40:07.341Z</Expires> 
    </Timestamp> 
    <o:UsernameToken Id="uuid-ac5ffd20-8137-4524-8ea9-3f4f55c0274c-12"> 
     <o:Username>someusername</o:Username> 
     <o:Password o:Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">somepassword 
    </o:Password> 
</o:UsernameToken> 
</o:Security> 

我们需要开始记录该客户端的IP地址,以及做一些IP白名单/黑名单。不幸的是,我不明白在这门课程中如何检索IP。在运行时,OperationContext.CurrentHttpContext.Current属性均为空。

+0

您是否尝试过使用'HttpContext.Current.Request.UserHostAddress i'或'string ipAddress = HttpContext.Current.Request.ServerVariables [“REMOTE_ADDR”];' – MethodMan 2013-04-05 17:23:51

+0

当在此UserNamePasswordValidator时,运行时为HttpContext.Current为null – csauve 2013-04-05 17:34:30

+0

当你通过代码并运行它时它会返回你当前的用户名和密码..?尝试调试,也许你得到的异常没有被提出 – MethodMan 2013-04-05 17:37:09

回答

1

正确答案是打开的asp.net兼容性得到HTTP上下文回来。很高兴我能帮上忙。

1

您可以打开ASP.NET兼容模式,这会让您访问HTTPContext对象。另一种方法是OperationContext。这是一个有点古怪,但你可以使用下面的代码以获得相同的:

var client = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; 
Console.WriteLine(client.Address); //This will get the IP Address 
Console.WriteLine(client.Port); //This gets the port that the client has open 
+0

嗨,Wiktor在评论中注意到了asp.net compat模式,所以我给了他答案。另外,正如问题所述,在我需要的地方,OperationContext.Current为null。 – csauve 2013-04-05 20:53:51

+0

@csauve我会猜测并说您没有在ServiceContract属性上设置SessionMode = SessionMode.Required属性? – Justin 2013-04-05 21:02:08

+0

@Justin我已经设置SessionMode = SessionMode.Required,但操作上下文仍然为空。 – VikciaR 2015-04-13 13:46:10