使用Windows身份验证的Ajax启用WCF服务适用于本地但不适用于托管

时间:2013-09-05 11:52:30

标签: c# asp.net ajax wcf iis-7

我有一个使用Windows身份验证的WCF ajax启用的Web服务,当我在我的本地PC上运行该应用程序时,它运行正常,但是当我托管它时,不希望将客户端凭据传递给服务。

我一直收到这个错误: 用户' NT AUTHORITY \ ANONYMOUS LOGON'

登录失败

我相信我已经包含了所有必要的配置,但我不确定。

我的网站配置:

    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <system.web>
    <authentication mode="Windows"/>
    <customErrors mode="Off"/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <connectionStrings>
    <add name="reportEntities" connectionString="metadata=res://*/Entities.TNTReport.csdl|res://*/Entities.TNTReport.ssdl|res://*/Entities.TNTReport.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=UECZATNT02;initial catalog=report;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <!--<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="PassThrough">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="TNTQueryEngine.Services.InformationServicesAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="TNTQueryEngine.Services.InformationServices">
        <endpoint address="" behaviorConfiguration="TNTQueryEngine.Services.InformationServicesAspNetAjaxBehavior"
          binding="webHttpBinding" contract="TNTQueryEngine.Services.InformationServices" />
      </service>
    </services>
  </system.serviceModel>-->
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="MyBindingForWindowsAuth">
          <security mode="TransportCredentialOnly">
            <!-- For Ntlm to work do => IIS right click Win Auth => Providers => Move NTLM up  -->
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceAuthorization principalPermissionMode="UseAspNetRoles" impersonateCallerForAllOperations="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="XYZ">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="TNTQueryEngine.Services.InformationServices" behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyBindingForWindowsAuth"
                  contract="TNTQueryEngine.Services.InformationServices" behaviorConfiguration="XYZ" />
        <endpoint address="mex" binding="webHttpBinding" bindingConfiguration="MyBindingForWindowsAuth" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

我的WCF服务有1种方法:

namespace TNTQueryEngine.Services
{
    [ServiceContract(Namespace = "InfoServices")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class InformationServices 
    {
        reportEntities RP = new reportEntities();
        [OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
        [OperationBehavior(Impersonation = ImpersonationOption.Required)]
        public List<Receiving> GETReceiving(DateTime DATE)
        {
            return (from SS in RP.spQueryEngineGETReceivingSummary(DATE)
                    where SS.QTY > 0
                    select new Receiving
                    {
                        ReceivedDate = DATE,
                        Model = SS.Model,
                        Qty = (int)SS.QTY
                    }
                    ).ToList();
        }

    }
}

真的不确定会出现什么问题。在此先感谢您的帮助

0 个答案:

没有答案