几天来,我一直在尝试让自定义UserNamePasswordValidator与我的netHttpsBinding 一起用于 .Net 4.5 WCF Web 服务。我的问题是验证器永远不会被调用或强制执行,因此任何凭据(或没有凭据)都可以访问。
我很确定配置是正确的,因为我可以为 wsHttpBinding 使用几乎相同的配置并且它可以工作。这是web.config ...
<services>
<service name="MyNamespace.MyService" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="MyNamespace.IMyService" />
<!-- This one doesn't authenticate
<endpoint address="" binding="netHttpsBinding" contract="MyNamespace.IMyService" />
-->
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding>
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
<netHttpsBinding>
<binding>
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</netHttpsBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyNamespace.MyValidator, MyAssembly"/>
</serviceCredentials>
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
如果这很重要,这里是验证器......
using System.IdentityModel.Selectors;
using System.ServiceModel;
namespace MyNamespace
{
public class MyValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (null == userName || null == password)
throw new FaultException("Username or Password were not set.");
if (!(userName == "MyUser" && password == "MyPassword"))
throw new FaultException("Unknown Username or Incorrect Password");
}
}
}
有谁知道为什么 UserNamePasswordValidator 不能与 netHttpsBinding 一起使用?或者我做错了什么?