2014-09-11 43 views
2

當閱讀有關基本驗證我總能找到類似這樣的例子:結腸字符AuthenticationHeaderValue

HttpClient sClient new HttpClient(); 
sClient.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Basic",Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("UserName:Password"))); 

對我來說這似乎意味着冒號「:」不要在密碼中使用。這是否意味着冒號字符應該被禁止輸入密碼和用戶名?

這是基本認證的一般假設嗎?如果您擁有現有的用戶基礎,並且憑據中包含現有的冒號字符,該如何處理此問題。

回答

3

技術上,:在用戶名禁止:https://www.ietf.org/rfc/rfc2617.txt

To receive authorization, the client sends the userid and password, 
separated by a single colon (":") character, within a base64 [7] 
encoded string in the credentials. 

    basic-credentials = base64-user-pass 
    base64-user-pass = <base64 [4] encoding of user-pass, 
        except not limited to 76 char/line> 
    user-pass = userid ":" password 
    userid  = *<TEXT excluding ":"> 
    password = *TEXT 

值得注意的是:RFC2617不說,如果用戶名包含一個冒號做什麼。 IE和Firefox都只是簡單地包含它,可能是最糟糕的選擇。

如果您有一個包含:的用戶名,您應該%將其轉換爲%3a以將其與分隔符區分開來。服務器是否支持這取決於服務器。儘管如此,HTTP Basic是一個非常糟糕的認證方案,使用能夠更好地保護客戶憑據的內容是更好的選擇。