WCF サービスにユーザー名認証を実装する方法については、この MSDN の記事に従っています。
手順 5 では、attribute: [AspNetCompatibilityRequirements]
ASP.NET 互換モード用に WCF サービスを構成するためにサービスに を設定します。認証は HTTP モジュールによって行われ、WCF で命令的または宣言的にユーザーを承認するには、HTTP コンテキストからプリンシパルと ID を取得できる必要があるため、これが必要になります。
サービスを実行すると、次のエラー メッセージが表示されます。
ASP.NET 互換性をサポートしていないため、サービスをアクティブ化できません。このアプリケーションでは、ASP.NET 互換性が有効になっています。web.config で ASP.NET 互換モードをオフにするか、AspNetCompatibilityRequirements 属性をサービス タイプに追加して、RequirementsMode 設定を 'Allowed' または 'Required' に設定します。
属性を明示的に宣言したにもかかわらず、無視されているようです。この理由は何ですか?値を 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>