在WCF SOAP UserNamePasswordValidator中确定客户端的IP地址

时间:2013-04-05 17:12:43

标签: c# wcf authentication soap

我们目前正在使用自定义UserNamePasswordValidator。这允许使用者在SOAP标头中指定凭据,如下所示:

<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属性都为空。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

正确答案是打开asp.net兼容性以获取http上下文。很高兴我能提供帮助。