首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WCF中以编程方式创建WebChannel时应用基本安全性?

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

Stack Overflow用户
提问于 2011-08-31 20:42:00
回答 1查看 782关注 0票数 2

我正在使用WebChannelFactory在代码中为POX web客户端创建WCF通道。我已经用基本身份验证集在我的app.config中创建了一个绑定配置,但是当我试图连接到服务端点时,基本安全性没有被应用,我从服务器得到了401。

我的app.config端点配置和编程声明匹配中的名称。我可以确认这张提单,它正确地记下了地址。

服务端点对基本安全性提出了挑战,但什么也没有发生。

是否需要设置wcf客户端endpointConfiguration?

代码语言:javascript
复制
 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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2011-09-01 13:54:49

添加一个endpointConfiguration解决了这个问题。现在正在发送身份验证头。

代码语言:javascript
复制
       ...
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7263135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档