我正在关注这篇关于如何为 WCF 服务实现用户名身份验证的 MSDN 文章。
在第 5 步中,attribute: [AspNetCompatibilityRequirements]
在服务上设置一个以将 WCF 服务配置为 ASP.NET 兼容模式。这是必需的,因为身份验证由 HTTP 模块完成,并且您必须能够从 HTTP 上下文中获取主体和身份,以便在 WCF 中以命令式或声明方式授权用户。
当我运行该服务时,我收到此错误消息:
该服务无法激活,因为它不支持 ASP.NET 兼容性。为此应用程序启用了 ASP.NET 兼容性。在 web.config 中关闭 ASP.NET 兼容模式,或将 AspNetCompatibilityRequirements 属性添加到服务类型,并将RequirementsMode 设置为“允许”或“必需”。
似乎即使我明确声明了它被忽略的属性。这可能是什么原因?即使我将值更改为 AspNetCompatibilityRequirementsMode.Allowed 它也不起作用。同样的错误消息很奇怪,因为 IIS 没有理由抱怨!
服务:
namespace MyNamespace.IISServiceHost
{
[ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CompanyNameAPIService
{
public CompanyNameAPIService()
{
}
}
}
界面
namespace MyNamespace
{
[ServiceContract]
public interface ICompanyAPI
{
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role="WSIuser")]
[ServiceKnownType(typeof(Supplier))]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void AddUpdateSuppliers(int companyId, Supplier[] sups);
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role = "WSIuser")]
[ServiceKnownType(typeof(Dimension))]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void AddUpdateDimension(int companyId, Dimension dims);
...
}
}
我还在Web.config中设置了等效项。
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="True" multipleSiteBindingsEnabled="true" />
<service behaviorConfiguration="ServiceBehavior" name="MyNamespace.IISServiceHost.CompanyAPIService">
<endpoint address="" behaviorConfiguration="largeDataBehavior"
binding="basicHttpBinding" bindingConfiguration="BasicBindingConfiguration"
name="BasicEndpoint" contract="MyNamespace.ICompanyAPI" />
<endpoint address="help" behaviorConfiguration="helpPageBehavior"
binding="mexHttpsBinding" bindingConfiguration="" name="MexEndpoint"
contract="MyNamespace.ICompanyAPI"
kind="mexEndpoint" endpointConfiguration="" />
</service>
<bindings>
<basicHttpBinding>
<binding name="BasicBindingConfiguration"><security mode="Transport" /></binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding">
<security mode="Transport"><transport clientCredentialType="None" /></security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="largeDataBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<enableWebScript />
</behavior>
<behavior name="helpPageBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpsGetEnabled="true" httpsGetUrl="https://localhost/WSI/service.svc/wsdl" />
<serviceDebug httpsHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
<serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="WSIRoleProvider">
<authorizationPolicies>
<add policyType="myNamespace.AuthorizationPolicy.HttpContextPrincipalPolicy, AuthorizationPolicy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</authorizationPolicies>
</serviceAuthorization>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>