c# - AuthenticationHeaderValue 中的冒号字符

标签 c# authentication

在阅读基本身份验证时,我总是找到与此类似的示例:

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/

相关文章:

c# - 模拟内存泄漏

php - Zend_Auth 最佳实践

authentication - 无需 Kerberos 或 AD/LDAP 的 Hbase 身份验证

session - 如何在 clojure 网络应用程序中创建用户帐户?

apache - 2 Way SSL using Apache - 证书问题

c# - 使用 varbinary 使用 FileUpload 将图像检索到数据库

c# - Azure Durable 编排功能触发两次

c# - 如何从.NET调用Windows 7自带的 "Print Pictures"应用程序

c# - EditorTemplates - 在 Date.cshtml 和 DateTime.cshtml 之间共享实现

python - 代理身份验证错误 - python