[英]WCF 4.0's analog to WCF REST Starter Kit's RequestInterceptor?
[英]How to validate the Content-MD5 header (from the actual content) in a RequestInterceptor (WCF REST Starter Kit) - REST web service
我正在通過WCF REST入門套件來實現REST Web服務。
我在System.ServiceModel.Channels.RequestContext中收到請求。
具體來說:攔截器以這種方式啟動:Public Overrides Sub ProcessRequest(ByRef requestContext As RequestContext)
如果請求中包含Content-MD5標頭,則必須針對實際的內容主體驗證提供的哈希,對嗎? 因為這不會“自動”發生。 沒有人(IIS或任何人)為我驗證這一點,因為我最初認為這會發生。
我認為進行此內容驗證會很容易。 我只需要獲取請求主體作為字符串,並將我的GenerateChecksumForContent()的結果與標頭中包含的哈希值進行比較即可。
如何根據內容計算MD5:
Public Shared Function GenerateChecksumForContent(ByVal content As String) As String
' Convert the input string to a byte array and compute the hash.
Dim hashed As Byte() = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(content))
' Convert the hash to a Base64 Encoded string and return it
Return Convert.ToBase64String(hashed)
End Function
如何獲取Content-MD5請求標頭值:
Dim message As Message = requestContext.RequestMessage
Dim reqProp As HttpRequestMessageProperty = DirectCast(message.Properties(HttpRequestMessageProperty.Name), HttpRequestMessageProperty)
Dim contentMD5HeaderValue As String = reqProp.Headers("Content-MD5")
我的問題是,我不知道如何做看起來很簡單的事情,例如計算請求正文的Content-MD5。
我找不到任何內置屬性可以告訴我此信息(內容的MD5當前哈希值)。
我已經嘗試過了,但是不起作用:
Dim content As String = requestContext.RequestMessage.GetBody(Of String)()
Dim computedMD5 As String = GenerateChecksumForContent(content)
另外,在RequestInterceptor運行並且“真實”方法處理內容之后會發生什么? 內容會因為已經被讀取而丟失?
我是否應該另外執行“ .CreateBufferedCopy()”之類的操作,以使請求主體可用於后RequestInterceptor處理?
我不明白為什么這么復雜! 這應該是微不足道的,但是如您所見,我完全迷失了。
請有人幫我...
非常感謝,
霍拉西奧-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.