我正在(拼命地)让客户身份验证提供程序为我的WCF服务工作。到目前为止,我有以下代码;
的web.config
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyNamespace.CustomUserNameValidator, MyNamespace" />
</serviceCredentials>
<wsHttpBinding>
<binding name="wsHttpBindingConfig" >
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
自定义身份验证器类;
public class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
// have security details been provided?
if (null == userName || null == password)
{
throw new ArgumentNullException();
}
// authenticate user
if (!(userName == "test" && password == "test"))
{
// This throws an informative fault to the client.
throw new FaultException("SecurityFailed");
}
}
}
所有内容都可以正常编译,但是当我使用Visual Studio中的WCF测试客户端调用名为Ping的方法(下面)时,自定义身份验证器永远不会被使用。 Ping方法只执行我在CustomUserNameValidator类中的任何断点。
为什么会这样?所有帮助表示赞赏。
答案 0 :(得分:1)
在您拥有的行
customUserNamePasswordValidatorType="MyNamespace.CustomUserNameValidator, MyNamespace"
该类型的第二部分(您当前拥有“MyNamespace”)应该是包含该类型的程序集的名称,不带任何文件扩展名。
有关更多帮助,请参阅this question。
答案 1 :(得分:0)
一些建议。
当我使用CustomAuthentication时,我将绑定设置为:
<wsHttpBinding>
<binding name="wsHttpBindingConfig" >
<security mode="TransportWithMessageCredentials">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
我认为你会喜欢某种形式的安全保障。使用自签名SSL证书在您的计算机上进行测试非常简单。有关如何执行此操作的更多信息here。
答案 2 :(得分:0)
我已经获取了您提供的所有信息并创建了一个模板web.config。我希望它看起来像这样。
<system.serviceModel>
<services>
<service name="<YourNameSpace>.<ServiceName>" <behaviorConfiguration="<YourNameSpace>.<BehaviorName>">
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="wsHttpBindingConfig"
contract="<YourNameSpace>.<ServiceInterface>"
/>
<!--Notice the binding is mexHttpsBinding, default is http-->
<endpoint
address="mex"
binding="mexHttpsBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="<YourNameSpace>.<BehaviorName>">
<!--Notice the httpsGetEnabled, default is http-->
<serviceMetadata httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="<YourNameSpace>.CustomUserNameValidator, <YourNameSpace>"
/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBindingConfig">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
<wsHttpBinding>
</bindings>