从ASP.NET调用WCF服务时身份验证失败

时间:2011-09-21 18:49:12

标签: asp.net wcf wcf-security

平台:VS 2008,.NET 3.5,C#,Oracle 11g

我创建了一个WCF服务,它接受一些数据元素,然后将它们插入到数据库表中并返回一个整数。我还创建了一个小的ASP.NET Web应用程序来测试该服务。测试Web应用程序只有一个包含字段和按钮的页面,单击该按钮实际调用Web服务以插入数据并返回整数值。

我采取的步骤:

  • 构建WCF服务
  • 发布WCF服务
  • 使用svcutil
  • 生成代理类(.cs)和app.config
  • 构建测试asp.net应用程序并添加上述步骤中生成的代理类和配置设置。
  • 破坏测试应用

在我的计算机上部署WCF和测试Web应用程序时,它运行正常 - Windows XP,IIS 5.1。但是,每当我尝试在远程服务器上部署它时,它都不起作用。当我尝试使用该服务(部署在远程服务器上 - Windows 2003服务器,IIS 6)时,我收到以下错误:

  

无法满足安全令牌的请求,因为   身份验证失败。

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.ServiceModel.FaultException:请求   无法满足安全令牌,因为身份验证失败。

以下是.config文件内容:

调用ASP.NET Web应用程序(消费者)的Web.Config的

wcf部分:

  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
    <binding name="WSHttpBinding_IMyWCFService" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
        allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
          enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
        realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
        algorithmSuite="Default" establishSecurityContext="true" />
      </security>
    </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://57.23.85.28:8001/MyWCFService/MyWCFService.svc"
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMyWCFService"
      contract="IMyWCFService" name="WSHttpBinding_IMyWCFService">
    <identity>
      <dns value="localhost" />
    </identity>
      </endpoint>
    </client>
  </system.serviceModel>

WCF的Web.Config:

<configuration>
  <connectionStrings>
    <add name="DSMyWCF" connectionString="Data Source=XXX;User id=XXX;Password=XXX;"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="MyWCFService.MyWCFServiceBehavior"
        name="MyWCFService.MyWCFService">
        <endpoint address="" binding="wsHttpBinding" contract="MyWCFService.IMyWCFService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/Design_Time_Addresses/MyWCFService/MyWCFService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyWCFService.MyWCFServiceBehavior">
          <!-- To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes,
          set the value below to true.  Set to false before deployment
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>    

1 个答案:

答案 0 :(得分:1)

可能与wcf服务的安全配置有关,具体而言,Windows凭据类型需要有效的域用户名和密码信息。

尝试在客户端上提供以下属性;

proxy.ClientCredentials.Windows.ClientCredential.UserName = "UserName ";
proxy.ClientCredentials.Windows.ClientCredential.Password = "Password ";
proxy.ClientCredentials.Windows.ClientCredential.Domain = "Domain ";