Wcf 自定义 userNamePasswordValidationMode 在 IIS Express 上不起作用

标签 wcf ssl iis-express

我尝试使用自定义 userNamePasswordValidationMode 服务制作安全的 wcf,但我遇到了一些问题(三天)。我的设置环境是visual studio 2012、.net 4.0、IIS Express。我在主机中的服务模型是:

<system.serviceModel>
    <bindings >
      <wsHttpBinding>
        <binding maxReceivedMessageSize="65536" name="WsHttpBinding_ISurveyService">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>
          <readerQuotas   maxArrayLength="65536"
                          maxBytesPerRead="65536"
                          maxStringContentLength="65536"/>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <!--name= "namespace.serviceClass"-->
      <service name="Rids.Services.SurveyService" behaviorConfiguration="Rids.WcfHost.ServiceBehavior" >
        <!--contract= "namespace.serviceClass"-->
        <endpoint address=""
                  binding="wsHttpBinding"
                  bindingConfiguration="WsHttpBinding_ISurveyService"
                  contract= "Rids.Services.ISurveyService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Rids.WcfHost.ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="None" />
            </clientCertificate>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
                                    customUserNamePasswordValidatorType="Rids.WcfHost.ServiceUserValidator,Rids.WcfHost"/>

            <!--Specify the Certificate-->
            <serviceCertificate    findValue="rids_2014.04.15"
                               storeLocation="LocalMachine"
                                x509FindType="FindBySubjectName"
                                   storeName="My"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

当我想将此服务添加到客户端时,它无法读取元数据并给出以下错误:

There was an error downloading 'https://localhost:44300/SurveyService.svc/_vti_bin/ListData.svc/$metadata'.
Unable to connect to the remote server
Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı 127.0.0.1:44300
Metadata contains a reference that cannot be resolved: 'https://localhost:44300/SurveyService.svc'.
There was no endpoint listening at https://localhost:44300/SurveyService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server
Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı 127.0.0.1:44300
If the service is defined in the current solution, try building the solution and adding the service reference again.

--

但是,如果我从行为(name="Rids.WcfHost.ServiceBehavior" 部分)中删除名称标签,并从服务(behaviorConfiguration="Rids.WcfHost.ServiceBehavior" 部分)中删除行为配置标签;然后我可以将服务(https://localhost:44300/SurveyService.svc)添加到客户端而不会出错。这也不能解决我的问题。

为客户端添加服务后,客户端服务模型如下:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ISurveyService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:11067/SurveyService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISurveyService"
        contract="Survey.ISurveyService" name="BasicHttpBinding_ISurveyService" />
    </client>
</system.serviceModel>

我看到的问题是:

  1. 尽管我的服务绑定(bind)配置是 wsHttpBinding,但客户端服务模型看到的是 basicHttpBinding。
  2. 虽然我在路径中添加了服务,但客户端服务模型看不到 ssl 地址:https://localhost:44300/SurveyService.svc .
  3. 如果服务具有 behaviorConfiguration 标签并且行为在主机配置中具有名称标签,我无法将服务添加到客户端。
  4. 自定义 userNamePasswordValidationMode 不起作用,即使我无误地向客户端添加服务,服务也看不到验证类或验证方法。它无需验证即可工作。

注意:“启用 SSL”的服务宿主项目属性为 True;并在项目配置项目 url 设置为 https://localhost:44300/ .

我遇到过问题中的问题。任何建议或解决方案?提前致谢。

最佳答案

我学会了如何解决这个问题。我使用 Visual Studio 2012 中的 EditWcfConfiguration 工具实现了它,如下所述。

首先,在宿主项目属性中将“启用 SSL”的属性设置为 true。在此属性下将自动生成 ssl url(例如'https://localhost:44300/')然后,右键单击宿主项目并选择属性。在项目属性 Web 选项卡中,选中“使用本地 IIS Web 服务器”,然后选中“使用 IIS Express”

-- 在宿主项目 WebConfig 中,右键单击然后选择 EditWcfConfiguration:

在EditWcfConfiguration中首先添加服务,然后为服务端点添加绑定(bind),然后为服务添加行为。

1- 添加服务

  • 在主机的 bin 文件夹中浏览服务类型
  • 选择通信方式为HTTP
  • 选择 Advanced Web Service interoperability 作为 Simplex communication
  • 地址留空
  • 添加行为配置后;选择行为配置
  • 添加绑定(bind)配置后;然后打开服务端点,选择 binding(customBinding) 和 bindingConfiguration

2- 添加自定义绑定(bind)

  • 重命名绑定(bind)以在服务的 BehaviorConfiguration 属性中使用(例如“custom_binding”)。
  • 添加安全、httpsTransport 标签。
  • 在安全标记下选择“AuthenticationMode”作为“UserNameOverTransport”。

3- 在 Advanced/Service Behaviors 文件夹中,添加新的服务行为配置

  • 重命名要在服务端点的 BindingConfiguration 属性中使用的行为(例如 safe_behavior)。
  • 添加 serviceCredentials、serviceMetadata、serviceDebug 标签。
  • 对于 serviceCredentials 标记属性:

    a-) 选择 customUserNamePasswordValidatorType 作为您的验证器类及其命名空间(例如:'Rids.WcfHost.ServiceUserValidator, Rids.WcfHost')

    b-) 选择 userNamePasswordValidationMode 作为自定义

  • 对于 clientCertificate 标签(在 serviceCredentials 标签下)属性,将 certificateValidationMode 选择为 None,将 revocationMode 选择为 NoCheck

  • 对于 serviceMetadata 标记属性,将 HttpsGetEnabled 设置为 True
  • 对于 serviceDebug 标记属性,将 includeExceptionDetailInFaults 设置为 True

经过这些步骤,得到的服务模型是:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="safe_behavior">
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="None" revocationMode="NoCheck" />
            </clientCertificate>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
              customUserNamePasswordValidatorType="Rids.WcfHost.ServiceUserValidator, Rids.WcfHost" />
          </serviceCredentials>
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="custom_binding">
          <security authenticationMode="UserNameOverTransport" />
          <httpsTransport />
        </binding>
      </customBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="safe_behavior" name="Rids.Services.Services.SurveyService">
        <endpoint address="" binding="customBinding"
          bindingConfiguration="custom_binding" contract="Rids.Services.Services.ISurveyService" />
      </service>
    </services>
  </system.serviceModel>

关于Wcf 自定义 userNamePasswordValidationMode 在 IIS Express 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103497/

相关文章:

apache - Xampp Localhost 上的 SSL 在 Windows 上使用 VirtualHost 重定向到 Xampp 默认信息/本地主机页面

python - 如何创建可以解码 SSL 流量的代理?

visual-studio - 在 Visual Studio 2015 Update 2 中停止调试后如何让 iisexpress 继续运行?

azure - 我可以在 Azure 中使用带有免费 azurewebsites.net 的自定义域吗?我还可以拥有免费的 ssl 吗?

wcf - NetNamedPipeBinding 绑定(bind)是否保证只能在同一台机器上工作?

wcf - ChannelFactory:创建和处置

wcf - 保存 SoapUI 项目时如何保存响应

asp.net-mvc-3 - 站点重定向到服务器上的错误登录页面,但在本地工作正常

asp.net - Launch URL和App URL有什么区别?

c# - 为什么处置/关闭 WCF 客户端代理很重要