如何在WCF中以编程方式创建WebChannel时应用基本安全性?

时间:2011-08-31 20:42:01

标签: wcf basic-authentication

我正在使用WebChannelFactory在代码中为POX Web客户端创建WCF频道。我在app.config中使用基本身份验证集创建了绑定配置,但是当我尝试连接到服务端点时,基本安全性未应用,我从服务器获得401。

我的app.config端点配置中的名称和我的程序化声明匹配。我可以确认这个b / c正确地拿起地址。

服务端点对BASIC安全性提出了挑战,但没有任何反应。

我是否需要设置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;
            }
        }

    }
}

配置

<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>

1 个答案:

答案 0 :(得分:0)

添加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>