【发布时间】:2015-07-11 19:38:21
【问题描述】:
正如标题所说,用户使用基本身份验证通过 SSL 登录并调用 WebService 方法,我想提取身份验证标头并在方法函数中使用用户名参数。是否有一种简单的方法可以内联(在方法本身内部)或作为单独项目中的类方法?
.NET 4.5、IIS 7.5
谢谢
经过一番搜索后,我发现了一些应该做我想做的代码:
string authHeader = WebClient.Headers[HttpRequestHeader.Authorization];
if (authHeader != null && authHeader.StartsWith("Basic"))
{
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));
int seperatorIndex = usernamePassword.IndexOf(':');
return usernamePassword.Substring(0, seperatorIndex);
}
但在编译 Visual Studio 时出现错误:非静态字段、方法或属性“System.Net.WebClient.Headers.get”需要对象引用。
我知道一旦我可以填充 authHeader 字符串,其他一切都会按预期工作,但我正在努力获取实际的标头值。
【问题讨论】:
-
对于客户端:
WebClient.Headers[HttpRequestHeader.Authorization]将为您提供可以转换为字符串的字节。 -
更新了问题以显示我目前在哪里处理它,似乎只是获取标题是症结所在。我还想澄清这是一个 WebService 而不是 WCF 项目。
标签: c# web-services authentication iis-7.5