2010-07-01 57 views
0

我正在通過WCF REST入門工具包實現REST Web服務。如何驗證RequestInterceptor(WCF REST入門工具包)中的Content-MD5頭(從實際內容) - REST Web服務

我在System.ServiceModel.Channels.RequestContext中獲取請求。

具體來說:攔截器開始是這樣的:如果該請求包括內容-MD5報頭公共覆蓋子的ProcessRequest(爲ByRef RequestContext的作爲的RequestContext)

,我必須驗證對實際內容主體所提供的散列,是嗎?因爲這不會「自動」發生。沒有人(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()」的內容,以保持請求主體可用於post-RequestInterceptor處理?

我不明白爲什麼這是如此複雜!這應該是一件小事,但正如你所看到的,我完全迷失了。

請人幫我...

非常感謝,

Horacio.-

+0

無回覆:( 這是因爲這是一個太難或太愚蠢的問題? 從本質上講,我必須做一些簡單: - 我是一個Web服務 - 我處理傳入的RequestContext對象 - 如果(請求具有內容-MD5標頭) { 我必須驗證它與實際內容包含在RequestContext中。怎麼樣? } – horacioj 2010-07-05 20:54:01

+0

我唯一關心的是驗證內容的MD5校驗和嗎?我認爲這是一個非常基本的「必須」做的事情。那麼爲什麼還要添加Content-MD5頭,然後在服務器端沒有驗證呢?我錯過了一些東西...... – horacioj 2010-07-08 14:55:10

回答