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

发布于 2024-12-02 21:16:44 字数 2533 浏览 0 评论 0原文

我正在使用 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>

I am creating a WCF channel for a POX web client in code using the WebChannelFactory. I have created a binding configuration in my app.config with basic authentication set, but when I try to connect to the service endpoint, basic security is not being applied and I get a 401 from the server.

The name in my app.config endpoint configuration and my programmatic declaration match. I can confirm this b/c it's picking up the address correctly.

The service endpoint challenges for BASIC security, but nothing happens.

Do I need to set the wcf client endpointConfiguration?

Code

 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦屿孤独相伴 2024-12-09 21:16:44

添加端点配置解决了该问题。现在正在发送身份验证标头。

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

Adding an endpointConfiguration fixed the issue. The authentication header is now being sent across.

       ...
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文