2012-02-24 71 views
1

我有一個非常簡單的WCF4 Restful Web服務,它使用WcfRestContrib來允許自定義基本身份驗證。當客戶端搶先提供用戶名和密碼時,它非常有用,但如果他們不這樣做,它將返回400錯誤請求響應,而不是挑戰客戶端提供憑據(通過401響應)。使用WebAuthenticationConfiguration時啓用401質詢響應

該服務使用聲明性方法通過裝飾具有Attributes的必需類來實現WcfRestContrib。這裏是我的ServiceContract聲明:

// Standard attributes here 
[WebAuthenticationConfiguration(typeof(WebBasicAuthenticationHandler), 
           typeof(SecurityValidator), 
           false, 
           "SearchService") 
] 
public class SearchService 

其中只有一個非常簡單的操作:

// Standard attributes here 
[OperationAuthentication] 
SearchResponse Fetch(SearchRequest request) 

而且我UserNamePasswordValidator看起來(雖然它可能並不重要,因爲它傳遞憑據時只被調用):

public override void Validate(string userName, string password) 
{ 
    // TODO: Insert login validation logic here. 

    // To indicate login failure, throw a FaultException 
    // throw new FaultException("Unknown Username or Incorrect Password"); 

    // Just return to indicate that the username and password are valid 
    return; 
} 

我試圖調試WcfRestContrib,發現WebBasicAuthenticationHandler類扔BasicAuthorizationException(其只是在框架代碼中被捕獲),但是,由於某些原因,它沒有將異常轉換爲401.

根據我在WcfRestContrib github site上看到的內容,有an issue posted by its author (Mike O'Brian)表示他希望看到它會返回除以外的任何其他401我讀取,因爲它目前返回401的挑戰。

如果此功能存在,那麼我錯過了什麼,或者我在做其他事情是否錯誤?

UPDATE

如果沒有辦法與WcfRestContrib要做到這一點,是有另一種方式來實現這一目標(除了使用IIS中的基於Windows的標準基本身份驗證)?我接受任何(其他)替代解決方案。

回答

1

我想通了。我需要裝飾我的ServiceContract與ErrorHandlerAttribute

// Standard attributes here 
[WebAuthenticationConfiguration(typeof(WebBasicAuthenticationHandler), 
          typeof(SecurityValidator), 
          false, 
          "SearchService"), 
ErrorHandler(typeof(WebErrorHandler))] 
public class SearchService 

只需通過添加[ErrorHandler(typeof(WebErrorHandler))]屬性服務,使所有的魔術發生。我知道它必須是簡單的東西,只是希望在將我的額頭放在桌子上之前,我會看到它。