[英]Make Wcf Service IntegratedWindowsAuthentication
当我在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>
请指教..
在.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>
此外,我可能会补充说,如果您的WCF服务需要身份验证,这意味着您需要使用真实用户帐户使用服务,或者您需要授予DOMAIN \\ CLIENTCOMPUTERNAME $帐户访问服务的权限 - 因此,也许适合许多人的解决方案可能是改变配置而不允许匿名访问(我的回答中没有讨论)。 不过,我确实有时会选择使用Windows(Kerberos)身份验证来保护我的WCF服务。
添加这个对我有用。
<bindings>
<webHttpBinding>
<binding>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
从.NET 4.0更新到.NET 4.5.2时出现此错误。 我更改了clientCredentialType
<security mode="TransportCredentialOnly">
<transport clientCredentialType="None"/>
</security>
至
<security mode="TransportCredentialOnly">
<transport clientCredentialType="InheritedFromHost"/>
</security>
但是,设置clientCredentialType =“Windows”同样有效。
<services>
<service name="Test.Service1" behaviorConfiguration="TestName">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding" contract="Test.IService1" />
</service>
</services>
它解决了我的问题。
像其他答案一样,我需要将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;
我添加了一个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>
我不完全确定原因,但是当我将'Factory'属性添加到我的.SVC文件(你需要将它显式拖到Visual Studio)时,一切正常 - 没有对Web.config中的默认设置进行任何更改!
我添加了Factory =“System.ServiceModel.Activation.WebServiceHostFactory”,所以我的.SVC文件来自:
<%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" %>
对此:
<%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
唯一的副作用似乎是,当您在浏览器中单击.SVC文件时,您会收到“未找到端点”错误,但无论如何,当您正确调用它时,该服务正常工作。 如前所述,我使用.NET 4.6( 简化WCF配置 )的默认Web.config,因此我可能还需要添加端点详细信息才能再次使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.