基本身份验证似乎没有安全性 header
Basic Authentication appears to have no security header
我写了一个非常简单的 WFCSerice,returns 提供了 Windows 用户名。这是客户端代码:
public Form1()
{
ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client();
s1.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
string str = s1.ReturnWindowsUsername();
InitializeComponent();
}
我可以使用 Fidddler 在 HTTP Header 中查看凭据:
我尝试对基本身份验证做同样的事情(访问另一个支持基本身份验证的 Web 服务)。这是客户端代码:
public Form1()
{
InitializeComponent();
ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client();
s1.ClientCredentials.UserName.UserName = "testuser";
s1.ClientCredentials.UserName.Password = "testpassword";
string str = s1.GetData(1);
}
这是使用基本身份验证时来自 Fiddler 的屏幕截图:
为什么使用基本身份验证时 header 中没有任何内容。基本身份验证服务似乎按预期工作。这是响应(有趣的是似乎有两个请求和两个响应):
基本身份验证适用于 HTTP
级别。一般流程是客户端请求资源,然后服务器发出质询,然后客户端发出一个包含 Authorization
header 的新请求。如果Authorization
header中的用户名和密码被服务器接受,客户端通常会在后续请求中添加header而无需再次执行request - challenge - re-request-with-authorization
步骤。
如果一切设置正确,您应该会在 Fiddler 中看到两个请求。
- 一个没有
Authorization
header 的请求。服务器对此请求的响应将是 401
并附有 WWW-Authenticate: Basic realm="your realm"
header。
- 然后您应该看到第二个请求,其中
Authorization
header 已从客户端发送。
这是我的环境中的示例:
如果您没有看到来自服务器的 401
质询,则基本身份验证设置不正确。
为了让服务代理提供 header,您需要将客户端绑定配置为使用 <transport clientCredentialType="Basic"/>
。或者这就是我所做的,谁知道 WCF 有无数的配置选项。
编辑:我在服务端使用了这个:
<bindings>
<basicHttpBinding>
<binding name="httpTransportCredentialOnlyBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
在客户端:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53156/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WcfTest_CBT.IService1"
name="BasicHttpBinding_IService1" />
</client>
我使用 basicHttpBinding
、TransportCredentialOnly
和 Basic
以便轻松测试,而无需 SSL 等麻烦
我写了一个非常简单的 WFCSerice,returns 提供了 Windows 用户名。这是客户端代码:
public Form1()
{
ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client();
s1.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
string str = s1.ReturnWindowsUsername();
InitializeComponent();
}
我可以使用 Fidddler 在 HTTP Header 中查看凭据:
我尝试对基本身份验证做同样的事情(访问另一个支持基本身份验证的 Web 服务)。这是客户端代码:
public Form1()
{
InitializeComponent();
ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client();
s1.ClientCredentials.UserName.UserName = "testuser";
s1.ClientCredentials.UserName.Password = "testpassword";
string str = s1.GetData(1);
}
这是使用基本身份验证时来自 Fiddler 的屏幕截图:
为什么使用基本身份验证时 header 中没有任何内容。基本身份验证服务似乎按预期工作。这是响应(有趣的是似乎有两个请求和两个响应):
基本身份验证适用于 HTTP
级别。一般流程是客户端请求资源,然后服务器发出质询,然后客户端发出一个包含 Authorization
header 的新请求。如果Authorization
header中的用户名和密码被服务器接受,客户端通常会在后续请求中添加header而无需再次执行request - challenge - re-request-with-authorization
步骤。
如果一切设置正确,您应该会在 Fiddler 中看到两个请求。
- 一个没有
Authorization
header 的请求。服务器对此请求的响应将是401
并附有WWW-Authenticate: Basic realm="your realm"
header。 - 然后您应该看到第二个请求,其中
Authorization
header 已从客户端发送。
这是我的环境中的示例:
如果您没有看到来自服务器的 401
质询,则基本身份验证设置不正确。
为了让服务代理提供 header,您需要将客户端绑定配置为使用 <transport clientCredentialType="Basic"/>
。或者这就是我所做的,谁知道 WCF 有无数的配置选项。
编辑:我在服务端使用了这个:
<bindings>
<basicHttpBinding>
<binding name="httpTransportCredentialOnlyBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
在客户端:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53156/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WcfTest_CBT.IService1"
name="BasicHttpBinding_IService1" />
</client>
我使用 basicHttpBinding
、TransportCredentialOnly
和 Basic
以便轻松测试,而无需 SSL 等麻烦