在阅读基本身份验证时,我总是找到与此类似的示例:
HttpClient sClient new HttpClient();
sClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic",Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("UserName:Password")));
对我来说,这似乎意味着冒号字符“:”不应在密码中使用。这是否意味着密码和用户名应禁止使用冒号字符?
这是基本身份验证的一般假设吗?如果您的现有用户库的凭据中存在冒号字符,如何处理此问题。
最佳答案
从技术上讲,:
在用户名中是被禁止的: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 是一个非常糟糕的身份验证方案;使用更好地保护客户端凭据的东西是更好的选择。
关于c# - AuthenticationHeaderValue 中的冒号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25784304/