【问题标题】:How do I get basic security to apply when programmatically creating WebChannel in WCF?在 WCF 中以编程方式创建 WebChannel 时如何应用基本安全性?
【发布时间】: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>

【问题讨论】:

    标签: wcf basic-authentication


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2010-12-15
      • 2011-12-12
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      相关资源
      最近更新 更多