gpt4 book ai didi

.net - 将 WSE 示例代码转换为 WCF

转载 作者:行者123 更新时间:2023-12-02 03:08:20 26 4
gpt4 key购买 nike

我对 WSE 和 WCF 都很陌生,我正在尝试使用 WCF 使用 Web 服务,但所有示例文档均适用于 VS2005 + WSE。此 Web 服务使用 WS-Security 1.0。我已经通过 Visual Studio 添加了服务引用,但我不知道如何在 WCF 中执行与以下代码等效的操作:

// 1. Initialize the web service proxy
PartnerAPIWse integrationFramework = new PartnerAPIWse();

// 2. Set the username/password. This is using the Username token of WS-Security 1.0
UsernameTokenProvider utp = new UsernameTokenProvider("username", "password");
integrationFramework.SetClientCredential<UsernameToken>(utp.GetToken());

// 3. Declare the policy
Policy policy = new Policy(new UsernameOverTransportAssertion());
integrationFramework.SetPolicy(policy);

最佳答案

花了一天的时间进行一些实验后,我弄清楚了如何转换此代码。关键是在 VS2008 正确设置 WCF 代理上的绑定(bind)。

  1. 添加指向 WSDL 的服务引用
  2. 打开 App.config/Web.config 并找到 system.serviceModel 部分。将默认肥皂绑定(bind)的安全模式更改为 TransportWithMessageCredential。这是更改后我的文件的样子:

            <basicHttpBinding>
    <binding name="SoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
    receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false"
    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
    useDefaultWebProxy="true">
    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    <security mode="TransportWithMessageCredential">
    <transport clientCredentialType="None" proxyCredentialType="None"
    realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    </binding>
    </basicHttpBinding>
  3. 将上面的示例代码更改为如下所示

    Dim integrationFramework As New SoapClient()
    integrationFramework.ClientCredentials.UserName.UserName = "username"
    integrationFramework.ClientCredentials.UserName.Password = "password"

TransportWithMessageCredential 相当于 WSE 3.0 下的 UsernameOverTransportAssertion 策略

关于.net - 将 WSE 示例代码转换为 WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/778673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com