【问题标题】:Make Wcf Service IntegratedWindowsAuthentication使 Wcf 服务集成 Windows 身份验证
【发布时间】:2013-02-22 06:49:10
【问题描述】:

当我在 IIS 中将 Windows 身份验证启用和匿名设置为禁用时,出现以下错误。

主机上配置的认证方案 ('IntegratedWindowsAuthentication') 不允许那些在 绑定“BasicHttpBinding”(“匿名”)。请确保 SecurityMode 设置为 Transport 或 TransportCredentialOnly。 此外,这可以通过更改身份验证来解决 此应用程序的方案通过 IIS 管理工具,通过 ServiceHost.Authentication.AuthenticationSchemes 属性,在 应用程序配置文件在 元素,通过更新 ClientCredentialType 上的属性 绑定,或者通过调整 AuthenticationScheme 属性 HttpTransportBindingElement。

我的Wcf Service的web.config如下...

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpEndpointBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint binding="basicHttpBinding" 
        bindingConfiguration="BasicHttpEndpointBinding"
        contract="Test.IService1" name="BasicHttpEndpoint" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceAuthenticationManager 
             authenticationSchemes="IntegratedWindowsAuthentication"/>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpBinding" scheme="http" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
         multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

请指教..

【问题讨论】:

  • 你没有发布你的 web.config...
  • 现在准备好了。请指教。
  • 我在您的配置中没有看到服务定义,只是一个客户端。如果这是您的服务的配置文件并且您使用的是 .NET 4.0+,那么您很可能会获得一个默认端点,该端点可能没有正确设置安全性。您还需要将在配置文件中创建的绑定分配给您的服务。
  • 谢谢。现在它没有错误。但是如何避免身份验证。意味着如何避免在通过 IIS 浏览服务时询问用户名和密码。请帮忙。
  • 如果该用户与机器登录用户相同,那么请推荐一种在通过 IIS 浏览 Wcf 服务时避免此身份验证的方法。

标签: c# .net wcf iis-7


【解决方案1】:

在 .Net 4.0+ 中,Simplified WCF configuration 部分中未根据每个服务明确设置配置时使用“匿名”配置。如果从 元素中删除 name="BasicHttpEndpointBinding",或者如果将该 元素复制为没有 name 属性的新元素,它将成为 WCF 服务将使用的默认匿名绑定。这在您需要提供和使用可能不具有相同配置的 WCF 服务的情况下通常很有用 - 但至少您可以为没有特定配置集的服务设置默认配置。默认/匿名概念也适用于 元素。

<bindings>
  <basicHttpBinding>
    <binding> <!--Notice, no name attribute set-->
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

另外,我可能会补充一点,如果您的 WCF 服务需要身份验证,这意味着您需要使用真实用户帐户使用该服务,或者您需要授予 DOMAIN\CLIENTCOMPUTERNAME$ 帐户访问该服务的权限- 因此,对于许多人来说,也许正确的解决方案可能是更改配置以允许匿名访问(我的回答中没有讨论)。尽管如此,我有时确实会选择使用 Windows (Kerberos) 身份验证来保护我的 WCF 服务。

【讨论】:

  • 感谢您的回复和解释。我的情况完全一样,但对于 webHttpBinding。
【解决方案2】:

添加这个对我有用。

        <bindings>
        <webHttpBinding>
            <binding>
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </webHttpBinding>
    </bindings>

【讨论】:

  • 这个在我的场景中不起作用,但 scradams 的回答确实 - 唯一的区别似乎是 scradam 使用 basicHttpBinding 并且这引用了 webHttpBinding。这些关键字有什么区别?
  • webHttpBinding 用于 REST JSON 服务(例如在 Web 应用程序中),basicHttpBinding 用于 SOAP。请参阅stackoverflow.com/questions/2650785/… 或有关不同绑定的 WCF 文档。
  • 这正是我所需要的——我一直在使用基于 SSL 类型身份验证的示例,虽然我已经更改为使用“TransportCredentialOnly”模式,但仍然收到错误消息,因为我有clientCredentialType 设置为“基本”。谢谢!
【解决方案3】:

从 .NET 4.0 更新到 .NET 4.5.2 时出现此错误。我从

更改了 clientCredentialType
<security mode="TransportCredentialOnly">
    <transport clientCredentialType="None"/>
</security>

<security mode="TransportCredentialOnly">
    <transport clientCredentialType="InheritedFromHost"/>
</security>

但是,设置 clientCredentialType="Windows" 也同样有效。

【讨论】:

    【解决方案4】:

    在使用已经存在的 WCF Web URL 时,我遇到了同样的问题。 我尝试了这里提到的所有答案,但最终只有两件事有所帮助。

    1. 更改“打开和关闭 Windows 功能”中的设置。

    在本地 IIS 服务器中启用匿名身份验证和 Windows 身份验证。

    【讨论】:

      【解决方案5】:
      <services>
            <service name="Test.Service1" behaviorConfiguration="TestName">
              <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding" contract="Test.IService1" />
            </service>
          </services>
      

      它解决了我的问题。

      【讨论】:

        【解决方案6】:

        与其他答案一样,我需要将 Web.config 中的绑定更新为:

        <basicHttpBinding>
          <binding name="basicHttpBindin1">
            <security mode="TransportCredentialOnly">
              <transport clientCredentialType="Windows" />
            </security>
          </binding>
        </basicHttpBinding>
        

        但我还需要更新绑定的实例化:

        var binding = new BasicHttpBinding { MaxReceivedMessageSize = 1000000, ReaderQuotas = { MaxDepth = 200 } };
        
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
        binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
        

        【讨论】:

          【解决方案7】:

          我添加了一个 webHttpBinding 并将我的端点指向它,安全设置需要工作。没有它,我的端点使用了默认的 WCF 配置绑定:

              <services>
            <service behaviorConfiguration="ServiceBehavior" name="Service">
              <endpoint address="" binding="webHttpBinding" contract="IService" />
              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
          </services>
          <behaviors>
            <serviceBehaviors>
              <behavior name="ServiceBehavior">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
              </behavior>
            </serviceBehaviors>
          </behaviors>
          <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
          <bindings>
            <webHttpBinding>
                <binding>
                  <!--Notice, no name attribute set-->
                  <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                  </security>
                </binding>
            </webHttpBinding>
          
          </bindings>
          

          【讨论】:

            【解决方案8】:

            我不完全确定为什么,但是当我将“Factory”属性添加到我的 .SVC 文件中时(您需要将其显式拖到 Visual Studio),一切正常正常 - 无需更改 Web.config 中的默认设置!

            我添加了 Factory="System.ServiceModel.Activation.WebServiceHostFactory" 所以我的 .SVC 文件来自这个:

            &lt;%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" %&gt;

            到这里:

            &lt;%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %&gt;

            唯一的副作用似乎是,当您在浏览器中单击 .SVC 文件时,您会收到“找不到端点”错误,但是当您正确调用该服务时,该服务仍然可以正常工作。如前所述,我使用的是带有 .NET 4.6 (Simplified WCF configuration) 的默认 Web.config,因此我可能还需要添加端点详细信息才能使其再次工作。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2010-10-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-08-26
              • 1970-01-01
              相关资源
              最近更新 更多