Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WCF SOAP UserNamePasswordValidator中确定客户端的IP地址_C#_Wcf_Authentication_Soap - Fatal编程技术网

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

C# 在WCF SOAP UserNamePasswordValidator中确定客户端的IP地址,c#,wcf,authentication,soap,C#,Wcf,Authentication,Soap,我们目前正在使用一个自定义的。这允许使用者在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&l

我们目前正在使用一个自定义的。这允许使用者在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>

2013-04-05T16:35:07.341Z
2013-04-05T16:40:07.341Z
someusername
密码

我们需要开始记录客户端的IP地址,以及执行一些IP白名单/黑名单。不幸的是,我不知道在这个类中如何检索IP。在运行时,和属性都为null

您可以打开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

正确的答案是打开asp.net兼容性以恢复http上下文。很高兴我能帮上忙。

您是否尝试过使用
HttpContext.Current.Request.UserHostAddress I
string ipAddress=HttpContext.Current.Request.ServerVariables[“REMOTE_ADDR”]
HttpContext.Current在此UsernamePasswordValidator中运行时为null当您单步执行代码并运行时,它是否返回您当前的用户名和密码。。?试着调试可能你得到的异常没有被提出当我进入UserNamePasswordValidator时用户名和密码确实被传递给它(它们是它的参数)在单步执行时,是否有代码跳转到的其他内容,例如
Page\u Load
是否有
PostBack
etcll如果您在那里看到它,可能您正在清除代码中的某个位置。查找类似于
new
的内容,您是否将值存储在
隐藏字段中
和/或会话变量中`您是否可以显示一些实际的.cs代码…?嘿,Wiktor在一篇评论中指出了asp.net compat模式,因此我将答案归功于他。另外,正如问题中所指出的,在我需要它的地方,OperationContext.Current是空的。@csauve我猜一下,你没有在ServiceContract属性上设置SessionMode=SessionMode.Required属性?@Justin我已经设置了SessionMode=SessionMode.Required,但是操作上下文仍然是空的。