在 WCF 中以编程方式创建 WebChannel 时,如何获得应用基本安全性?
我正在使用 WebChannelFactory
在代码中为 POX Web 客户端创建 WCF 通道。我在 app.config
中创建了一个带有基本身份验证集的绑定配置,但是当我尝试连接到服务端点时,基本安全性并未被应用,并且我从服务器收到 401。
我的 app.config
端点配置中的名称与我的编程声明匹配。我可以确认这个 b/c 它正确地获取了地址。
服务端点对 BASIC 安全性提出了挑战,但什么也没发生。
我需要设置 wcf 客户端端点配置吗?
代码
namespace AccountServices
{
[ServiceContract]
public interface IAccount
{
[OperationContract]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Xml, UriTemplate="?resourceId={resourceId}")]
XmlElement GetAccount(string resourceId);
}
public class AccountService
{
public XmlElement GetAccount(string resourceId, string userName, string password)
{
WebChannelFactory<ICPM> factory = new WebChannelFactory<IAccount>("AccountHttpClient");
if (!string.IsNullOrWhiteSpace(userName))
factory.Credentials.UserName.UserName = userName;
if (!string.IsNullOrWhiteSpace(password))
factory.Credentials.UserName.Password = password;
IAccount proxy = factory.CreateChannel();
try
{
return proxy.GetAccount(resourceId);
}
catch (System.ServiceModel.Security.MessageSecurityException securityEx)
{
throw;
}
}
}
}
配置
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="RawWebBinding" contentTypeMapper="">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic"
realm="Login" />
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="pox">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="https://ENDPOINTADDRESS/"
behaviorConfiguration="pox" binding="webHttpBinding" bindingConfiguration="RawWebBinding"
contract="AccountServices.IAccount" name="AccountHttpClient" kind=""
endpointConfiguration="" />
</client>
</system.serviceModel>
I am creating a WCF channel for a POX web client in code using the WebChannelFactory
. I have created a binding configuration in my app.config
with basic authentication set, but when I try to connect to the service endpoint, basic security is not being applied and I get a 401 from the server.
The name in my app.config
endpoint configuration and my programmatic declaration match. I can confirm this b/c it's picking up the address correctly.
The service endpoint challenges for BASIC security, but nothing happens.
Do I need to set the wcf client endpointConfiguration?
Code
namespace AccountServices
{
[ServiceContract]
public interface IAccount
{
[OperationContract]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Xml, UriTemplate="?resourceId={resourceId}")]
XmlElement GetAccount(string resourceId);
}
public class AccountService
{
public XmlElement GetAccount(string resourceId, string userName, string password)
{
WebChannelFactory<ICPM> factory = new WebChannelFactory<IAccount>("AccountHttpClient");
if (!string.IsNullOrWhiteSpace(userName))
factory.Credentials.UserName.UserName = userName;
if (!string.IsNullOrWhiteSpace(password))
factory.Credentials.UserName.Password = password;
IAccount proxy = factory.CreateChannel();
try
{
return proxy.GetAccount(resourceId);
}
catch (System.ServiceModel.Security.MessageSecurityException securityEx)
{
throw;
}
}
}
}
Config
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="RawWebBinding" contentTypeMapper="">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic"
realm="Login" />
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="pox">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="https://ENDPOINTADDRESS/"
behaviorConfiguration="pox" binding="webHttpBinding" bindingConfiguration="RawWebBinding"
contract="AccountServices.IAccount" name="AccountHttpClient" kind=""
endpointConfiguration="" />
</client>
</system.serviceModel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加端点配置解决了该问题。现在正在发送身份验证标头。
Adding an endpointConfiguration fixed the issue. The authentication header is now being sent across.