2013-04-05 62 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