簡體   English   中英

使用SoapUI通過基本身份驗證測試WCF

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

我是Web服務的新手。 我想用用戶名和密碼驗證編寫一個簡單的WCF。 使用SoapUI進行測試時,如果安全模式設置為無(無基本身份驗證),則可以成功調用它。 一旦將安全模式設置為消息(使用基本身份驗證),它就無法返回正確的結果。 兩者都可以使用C#客戶端成功調用。 我對StackOverflow應用了許多建議,但仍然無法獲得正確的結果。

  • 在SoapUI的“請求屬性”部分的“ Wss-Password類型”中,選擇選項“ PasswordText”。
  • negotiateServiceCredential = “真”
  • 選中標記“將默認的WSA添加到”
  • 在http設置中,選中“為傳出結果添加身份驗證信息”

是否需要進行其他任何設置,例如配置證書? 我期待着您的幫助。

設置詳細信息在下面列出。

當安全模式設置為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>

SoapUI日志:

DEBUG:嘗試1執行請求DEBUG:發送請求:POST /ServiceHello.svc HTTP / 1.1 DEBUG:接收響應:HTTP / 1.1 500內部服務器錯誤DEBUG:可以無限期保持連接INFO:[WSHttpBinding_IServiceHello.HelloWorld的響應:請求1],以3ms(576字節)為單位

安全模式的結果=無

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

這是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>

在查看應用程序的配置時,我假設以下內容:a)您的WCF服務使用.NET Framework 4.5或更高版本。 b)該服務托管在啟用了SSL安全性的IIS中。 c)您想使用WsHttpBinding。 d)認證是定制的。

因此,我給您以下建議:

IIS發布:

a)SSL安全性:啟用“需要SSL和客戶端證書”將其設置為“忽略”。 您如何在Web.config中配置它:

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

b)身份驗證:啟用“匿名身份驗證”並禁用其他身份驗證。

Web.config文件:

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

並且

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

試試吧,祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM