WCF customUserNamePasswordValidatorType

时间:2010-07-28 15:17:52

标签: wcf

我有一个customUserNamePasswordValidatorType,我想在IServiceBehavior ApplyDispatchBehavior()实现中的代码中设置服务凭据。

如果我在这样的代码中设置它,问题是验证器永远不会执行:

     serviceHostBase.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator =
            new BigIPUsernamePasswordValidator() ;
        serviceHostBase.Credentials.UserNameAuthentication.UserNamePasswordValidationMode =
            UserNamePasswordValidationMode.Custom;

如果我在配置中设置,那么它会:

<serviceCredentials> <userNameAuthentication userNamePasswordValidationMode="Custom" $customUserNamePasswordValidatorType="Quad.WCF.Security.BigIPUsernamePasswordValidator, Quad.WCF" /> </serviceCredentials>

行为被设置为服务类的属性,如果我设置断点,我可以看到上面的代码正在执行。

我的理解是配置中的所有内容都应该能够在代码中设置。但是在IServiceBehavior ApplyDispatchBehavior()实现中设置它时,这不起作用。

任何人都知道这是否应该有效,或者我应该通过代码以不同的方式做到这一点?

1 个答案:

答案 0 :(得分:0)

我已经实现了这个并且它有效:

ServiceHost customServiceHost = new ServiceHost(type);
customServiceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
customServiceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new MyCustomUserNameValidator();
customServiceHost.AddServiceEndpoint(typeof(IServiceContract), customBinding, "https://localhost:443");
customServiceHost.Open();

我的自定义验证器非常简单(仅限示例,请勿使用此类!)

public class MyCustomUserNameValidator: UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        // Example Only
        return;
    }
}

您是否获得了任何特定的例外情况,或者它似乎没有被调用?

相关问题