简体   繁体   English

使用SoapUI通过基本身份验证测试WCF

[英]Using SoapUI to test a WCF with Basic Authentication

I am new to web service. 我是Web服务的新手。 I want to write a simple WCF with username and password authentication. 我想用用户名和密码验证编写一个简单的WCF。 When using SoapUI for testing, if the security mode is set to none (without basic authentication), it can be called successfully. 使用SoapUI进行测试时,如果安全模式设置为无(无基本身份验证),则可以成功调用它。 Once the security mode is set to message (with basic authentication), it cannot return the correct result. 一旦将安全模式设置为消息(使用基本身份验证),它就无法返回正确的结果。 Both of them can be called successfully using C# client. 两者都可以使用C#客户端成功调用。 I have applied many suggestions on StackOverflow but still cannot get the correct result. 我对StackOverflow应用了许多建议,但仍然无法获得正确的结果。

  • In the 'Request Properties' in SoapUI in section 'Wss-Password Type' just select option 'PasswordText'. 在SoapUI的“请求属性”部分的“ Wss-Password类型”中,选择选项“ PasswordText”。
  • negotiateServiceCredential="true" negotiateServiceCredential = “真”
  • Check mark "Add default WSA To" 选中标记“将默认的WSA添加到”
  • In http setting check “add authentication information for outgoing result” 在http设置中,选中“为传出结果添加身份验证信息”

Is there any additional setting need to be done, like configure the certificate? 是否需要进行其他任何设置,例如配置证书? I am looking forward to your help. 我期待着您的帮助。

The setting details are listed below. 设置详细信息在下面列出。

When security mode is set to message, the return result is 当安全模式设置为message时,返回结果为

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
   <s:Header>
      <a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action>
   </s:Header>
   <s:Body>
      <s:Fault>
         <s:Code>
            <s:Value>s:Sender</s:Value>
            <s:Subcode>
               <s:Value xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">a:InvalidSecurity</s:Value>
            </s:Subcode>
         </s:Code>
         <s:Reason>
            <s:Text xml:lang="zh-HK">An error occurred when verifying security for the message.</s:Text>
         </s:Reason>
      </s:Fault>
   </s:Body>
</s:Envelope>

The SoapUI log: SoapUI日志:

DEBUG:Attempt 1 to execute request DEBUG:Sending request: POST /ServiceHello.svc HTTP/1.1 DEBUG:Receiving response: HTTP/1.1 500 Internal Server Error DEBUG:Connection can be kept alive indefinitely INFO:Got response for [WSHttpBinding_IServiceHello.HelloWorld:Request 1] in 3ms (576 bytes) DEBUG:尝试1执行请求DEBUG:发送请求:POST /ServiceHello.svc HTTP / 1.1 DEBUG:接收响应:HTTP / 1.1 500内部服务器错误DEBUG:可以无限期保持连接INFO:[WSHttpBinding_IServiceHello.HelloWorld的响应:请求1],以3ms(576字节)为单位

Result for security mode=none 安全模式的结果=无

 <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
   <s:Header>
      <a:Action s:mustUnderstand="1">http://tempuri.org/IServiceHello/HelloWorldResponse</a:Action>
   </s:Header>
   <s:Body>
      <HelloWorldResponse xmlns="http://tempuri.org/">
         <HelloWorldResult>Hello World</HelloWorldResult>
      </HelloWorldResponse>
   </s:Body>
</s:Envelope>

And this is the web.config 这是web.config

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFTest.ServiceHello"
               behaviorConfiguration="WCFTest_Behavior">
        <endpoint 
          address="" 
          binding="wsHttpBinding" 
          contract="WCFTest.IServiceHello"
          bindingConfiguration="WCFTest_Config">
        </endpoint>

      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="WCFTest_Config">
<security mode="None">
<!—The only difference between two web service is the security mode, which is set to message in the authentication version -->            
            <message clientCredentialType="UserName" negotiateServiceCredential="false"
            establishSecurityContext="false" algorithmSuite="Default"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFTest_Behavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="None"/>
            </clientCertificate>
            <userNameAuthentication userNamePasswordValidationMode="Custom"                  customUserNamePasswordValidatorType="WCFTest.App_Code.Authentication.CustomValidator,App_Code/Authentication"/>
            <serviceCertificate 
              findValue="myCertificate"
              storeLocation="LocalMachine"
              storeName="My"
              x509FindType="FindBySubjectName" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

Reviewing the configuration of your application, I assume the following: a) Your WCF service uses .NET Framework 4.5 or later. 在查看应用程序的配置时,我假设以下内容:a)您的WCF服务使用.NET Framework 4.5或更高版本。 b) The service is hosted in IIS, with SSL security enabled. b)该服务托管在启用了SSL安全性的IIS中。 c) You want to use WsHttpBinding. c)您想使用WsHttpBinding。 d) Authentication is customized. d)认证是定制的。

Therefore, I give you the following recommendations: 因此,我给您以下建议:

IIS publication: IIS发布:

a) SSL Security: Enable Require SSL and Client Certificate set it to Ignore. a)SSL安全性:启用“需要SSL和客户端证书”将其设置为“忽略”。 Which is how you are configuring it in the Web.config: 您如何在Web.config中配置它:

<clientCertificate>
    <authentication certificateValidationMode = "None" />
</ clientCertificate>

b) Authentication: Enable "Anonymous Authentication" and disable others. b)身份验证:启用“匿名身份验证”并禁用其他身份验证。

Web.config: Web.config文件:

<wsHttpBinding>
   <binding name = "WCFTest_Config">
      <security mode = "TransportWithMessageCredential">
         <transport clientCredentialType = "None" proxyCredentialType = "None" realm = "" />
         <message clientCredentialType = "UserName" />
      </security>
   </binding>
</wsHttpBinding>

and also 并且

<protocolMapping>
   <add binding = "wsHttpBinding" scheme = "https" />
</protocolMapping>

Try it and good luck! 试试吧,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM