我正在使用WebChannelFactory在代码中为POX web客户端创建WCF通道。我已经用基本身份验证集在我的app.config中创建了一个绑定配置,但是当我试图连接到服务端点时,基本安全性没有被应用,我从服务器得到了401。
我的app.config端点配置和编程声明匹配中的名称。我可以确认这张提单,它正确地记下了地址。
服务端点对基本安全性提出了挑战,但什么也没有发生。
是否需要设置wcf客户端endpointConfiguration?
码
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>发布于 2011-09-01 13:54:49
添加一个endpointConfiguration解决了这个问题。现在正在发送身份验证头。
...
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="NewStandardEndpoint0">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic"
realm="Login" />
</security>
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
...
<client>
<endpoint address="https://ENDPOINTADDRESS/"
behaviorConfiguration="pox" binding="webHttpBinding" bindingConfiguration="RawWebBinding"
contract="AccountServices.IAccount" name="AccountHttpClient" kind="webHttpEndpoint"
endpointConfiguration="NewStandardEndPoint0" />
</client>https://stackoverflow.com/questions/7263135
复制相似问题