c# - 没有安全性的 BasicHttpBinding

标签 c# .net web-services wcf basichttpbinding

我对 WCF 非常陌生,并且我已经成功地能够使用 bacishttpbinding 来使用服务,我需要能够使用嗅探器查看服务和客户端之间的数据交换。为此,我假设我需要在绑定(bind)中没有安全性,每次我尝试在绑定(bind)中没有安全性时,都会得到以下信息: 提供的 URI 方案“https”无效;预期为“http”。

如何将服务和客户端配置为没有安全性,以便我可以嗅探纯 XML 中的数据传输?

该服务已配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <customErrors mode="Off"/>
</system.web>
<system.diagnostics>
    <sources>
        <source name="System.ServiceModel" switchValue="All" propagateActivity="true">
            <listeners>
                <add name="xml"/>
            </listeners>
        </source>
        <source name="System.ServiceModel.MessageLogging" switchValue="All">
            <listeners>
                <add name="xml"/>
            </listeners>
        </source>
    </sources>
    <sharedListeners>
        <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="D:\Technology\Production\wwwRoot\medical\log\Log.svclog"/>
    </sharedListeners>
    <!--
<sources>
  <source name="System.ServiceModel"
          switchValue="All"
          propagateActivity="true">
    <listeners>
      <add name="traceListener"
          type="System.Diagnostics.XmlWriterTraceListener"
          initializeData="D:\Technology\Production\wwwRoot\medical\log\Traces.svclog" />
    </listeners>
  </source>
</sources>
-->
</system.diagnostics>
<system.serviceModel>
    <diagnostics>
        <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" maxMessagesToLog="25000">
            <filters>
                <clear/>
            </filters>
        </messageLogging>
    </diagnostics>
    <behaviors>
        <serviceBehaviors>
            <behavior name="SecureBehave">
                <serviceCredentials>
                    <clientCertificate>
                        <authentication certificateValidationMode="PeerTrust"/>
                    </clientCertificate>
                    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="HIBridgeLib.HIBridgeService.Security.MessageSecurityValidator, HIBridgeLib"/>
                    <!--
        <serviceCertificate findValue="WCfServer"
          storeLocation="CurrentUser"
          storeName="My"
          x509FindType="FindBySubjectName" />
        -->
                </serviceCredentials>
                <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
                <serviceDebug includeExceptionDetailInFaults="True"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <bindings>
        <wsHttpBinding>
            <binding name="HIBridge_SSLBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
                <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="Certificate" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" negotiateServiceCredential="True" establishSecurityContext="True"/>
                </security>
            </binding>
        </wsHttpBinding>
        <basicHttpBinding>
            <binding name="HIBridge_BasicBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
                <security mode="Transport">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                </security>
                <readerQuotas maxStringContentLength="2147483647"/>
            </binding>
        </basicHttpBinding>
    </bindings>
    <services>
        <service name="HIBridgeWebService.HIBridgeService" behaviorConfiguration="SecureBehave">
            <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="HIBridge_BasicBinding" contract="HIBridgeLib.HIBridgeService.IHIBridgeService"></endpoint>
            <endpoint address="ws" binding="wsHttpBinding" bindingConfiguration="HIBridge_SSLBinding" contract="HIBridgeLib.HIBridgeService.IHIBridgeService"></endpoint>
            <host>
                <baseAddresses>
                    <add baseAddress="https://172.30.20.133:1125/HIBridge/HIBridgeService.svc"/>
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>
<connectionStrings>
    <clear/>
    <add name="DBConnectionString" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=HIBridge;User ID=CF;Password=C01dFu$i0n" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.webServer>
    <handlers accessPolicy="Read, Script"/>
    <directoryBrowse enabled="false"/>
    <defaultDocument>
        <files>
            <clear/>
            <add value="Default.aspx"/>
        </files>
    </defaultDocument>
    <httpErrors>
        <clear/>
    </httpErrors>
</system.webServer>
</configuration>

客户端配置如下:

System.ServiceModel.BasicHttpBinding basicHTTPSBinding = new System.ServiceModel.BasicHttpBinding();
            basicHTTPSBinding.Name = "HIBridge_BasicBinding";
            basicHTTPSBinding.OpenTimeout = TimeSpan.FromMinutes(1);
            basicHTTPSBinding.CloseTimeout = TimeSpan.FromMinutes(1);
            basicHTTPSBinding.SendTimeout = TimeSpan.FromMinutes(1);
            basicHTTPSBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            basicHTTPSBinding.BypassProxyOnLocal = false;

            basicHTTPSBinding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
            basicHTTPSBinding.MaxBufferPoolSize = 2147483647;
            basicHTTPSBinding.MaxReceivedMessageSize = 2147483647;
            basicHTTPSBinding.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text;
            basicHTTPSBinding.TextEncoding = Encoding.UTF8;
            basicHTTPSBinding.UseDefaultWebProxy = true;
            basicHTTPSBinding.AllowCookies = false;
            basicHTTPSBinding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
            basicHTTPSBinding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
            basicHTTPSBinding.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None;
            basicHTTPSBinding.Security.Transport.Realm = "";             

            System.ServiceModel.EndpointAddress endpointAddress = null;

            if (LocalMedCart.CartProfile.ConsoleHostname.Contains("/HIBridge/HIBridgeService.svc"))
                endpointAddress = new System.ServiceModel.EndpointAddress(LocalMedCart.CartProfile.ConsoleHostname + "/basic");
            else
                endpointAddress = new System.ServiceModel.EndpointAddress(string.Format("https://{0}:{1}/HIBridge/HIBridgeService.svc/basic", LocalMedCart.CartProfile.ConsoleHostname, LocalMedCart.CartProfile.CommunicationPort));


            HIBridgeLib.HIBridgeService.Security.PermissiveCertificatePolicy.Enact(string.Format("CN={0}", LocalMedCart.CertificateName));

            serviceProxy = new HIBridgeLib.HIBridgeService.HIBridgeServiceProxy(basicHTTPSBinding, endpointAddress);
            serviceProxy.ClientCredentials.UserName.UserName = "username";
            serviceProxy.ClientCredentials.UserName.Password = "password";

最佳答案

如果您更新 WCF 绑定(bind)以“关闭”安全性,则需要相应地更新基地址。

例如,更改以下内容:

<add baseAddress="https://172.30.20.133:1125/HIBridge/HIBridgeService.svc"/>

<add baseAddress="http://172.30.20.133:1125/HIBridge/HIBridgeService.svc"/>

关于c# - 没有安全性的 BasicHttpBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841721/

相关文章:

.net - Xamarin项目的Targeted Framework如何改成4.0

java - Spring MVC Soap Web 服务在 ws 调用后不返回数据

javascript - 这是通过 node.js 对 MongoDB 执行操作的最佳方式吗?

c# - 在 C# 可移植类库 (Microsoft.Net.Http) 中使用代理

javascript - 在javascript中得到错误的总数

c# - ResrSharp 将复杂模型发送到 API,可以通过 FromForm 读取

c# - Fusion Log Assembly Binder 错误 - 绑定(bind)结果 : hr = 0x1. 函数不正确

java - 从 alfresco 存储库加载 FTL 模板文件

c# - 如何从 AzureDevOps Pipelines 中的解决方案缓存每个项目的 project.assets.json?

c# - HttpListener : The requested address is not valid in this context