【发布时间】:2011-08-31 20:42:00
【问题描述】:
我正在使用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>
【问题讨论】: