制作Wcf服务IntegratedWindowsAuthentication

use*_*471 21 .net c# wcf iis-7

当我在IIS中设置Windows身份验证启用和匿名禁用时,我收到以下错误.

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

我的Wcf服务的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>
Run Code Online (Sandbox Code Playgroud)

请指教..

scr*_*dam 47

在.Net 4.0+中,如果未在<services>部分中基于每个服务显式设置配置,则简化的WCF配置将使用"匿名"配置.如果从<binding>元素中删除name ="BasicHttpEndpointBinding",或者如果将<binding>元素复制为没有name属性的新元素,它将成为WCF服务将使用的默认匿名绑定.这在您需要服务以及使用可能不具有相同配置的WCF服务的情况下通常很有用 - 但至少您可以为没有特定配置集的服务设置默认配置.默认/匿名概念也适用于<behavior>元素.

<bindings>
  <basicHttpBinding>
    <binding> <!--Notice, no name attribute set-->
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

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


小智 14

添加这个对我有用.

        <bindings>
        <webHttpBinding>
            <binding>
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </webHttpBinding>
    </bindings>
Run Code Online (Sandbox Code Playgroud)


Raj*_*air 0

<services>
      <service name="Test.Service1" behaviorConfiguration="TestName">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding" contract="Test.IService1" />
      </service>
    </services>
Run Code Online (Sandbox Code Playgroud)

它解决了我的问题。