2014-06-27 73 views
0

我要重写添加的注册功能时,已启用的默认登录验证。 我已经加入我自己的CustomRegistrationValidator为每ServiceStack文档(基本规则,现在,稍后扩大):ServiceStack自定义登记验证问题

public class CustomRegistrationValidator : RegistrationValidator 
    { 
     public CustomRegistrationValidator() 
     { 
      RuleSet(ApplyTo.Post,() => 
      { 
       RuleFor(x => x.FirstName).NotEmpty(); 
       RuleFor(x => x.LastName).NotEmpty(); 
       RuleFor(x => x.Email).NotEmpty(); 
       RuleFor(x => x.Password).NotEmpty(); 
      }); 
     } 
    } 

我已经在我的配置方法覆盖默认注册验证按照下面的代码片段:

Plugins.Add(new AuthFeature(
      () => new AuthUserSession(), 
        new IAuthProvider[] { new BasicAuthProvider(), 
        new CredentialsAuthProvider() 
       })); 

      Plugins.Add(new ValidationFeature()); 
      Plugins.Add(new RegistrationFeature()); 

      RegisterAs<CustomRegistrationValidator, IValidator<Register>>(); 
      container.RegisterValidators(typeof(AppHost).Assembly); 

然而,验证永远不会出现执行,因此我可以无效信息注册新用户。

我使用的是最新版本的ServiceStack V4.0.22

这是一个重大ServiceStack bug或者是此功能不再可用ServiceStack最新版本的?

预先感谢您的任何帮助。

问候 约翰

+0

ServiceStack版本4.0.35(2015年1月)仍具有自定义登记验证错误! (请参阅上面的文本)是否有解决方法或解决方法?***紧急修正需要*** – JohnC

+0

解决方法是在初始化完成后注册自定义验证器(请参阅我的答案)。你是否注册过ServiceStack这个问题? – dabide

回答

1

我可以证实,这仍是v4.0.35的错误。这是我周围如何得到...

  1. 创建一个实现AbstractValidator自定义登记验证类<注册>,并添加自己流利的验证规则(我复制从SS源的规则)

    public class CustomRegistrationValidator : AbstractValidator<Register> 
    { 
        public IAuthRepository UserAuthRepo { get; set; } 
        public CustomRegistrationValidator() 
        { 
         RuleSet(ApplyTo.Post,() => 
         { 
          RuleFor(x => x.UserName).NotEmpty().When(x => x.Email.IsNullOrEmpty()); 
          RuleFor(x => x.UserName) 
          .Must(x => UserAuthRepo.GetUserAuthByUserName(x) == null) 
          .WithErrorCode("AlreadyExists") 
          .WithMessage("UserName already exists") 
          .When(x => !x.UserName.IsNullOrEmpty()); 
          RuleFor(x => x.Email) 
          .Must(x => x.IsNullOrEmpty() || UserAuthRepo.GetUserAuthByUserName(x) == null) 
          .WithErrorCode("AlreadyExists") 
          .WithMessage("Email already exists") 
          .When(x => !x.Email.IsNullOrEmpty()); 
    
          RuleFor(x => x.FirstName).NotEmpty(); 
          RuleFor(x => x.LastName).NotEmpty(); 
          RuleFor(x => x.Email).NotEmpty(); 
          RuleFor(x => x.Password).NotEmpty(); 
          // add your own rules here... 
        }); 
    
        RuleSet(
         ApplyTo.Put, 
         () => 
         { 
          RuleFor(x => x.UserName).NotEmpty(); 
          RuleFor(x => x.Email).NotEmpty(); 
          // add your own rules here... 
         }); 
    } 
    

    }

  2. 创建一个实现了IPlugin一个CustomRegistrationFeature类(再次我刚才复制的SS源和改变在国际奥委会注册到CustomRegistrationValidator类)

    public class CustomRegistrationFeature : IPlugin { 
        public string AtRestPath { get; set; } 
    
        public CustomRegistrationFeature() 
        { 
         this.AtRestPath = "/register"; 
        } 
    
        public void Register(IAppHost appHost) 
        { 
         appHost.RegisterService<RegisterService>(AtRestPath); 
         appHost.RegisterAs<CustomRegistrationValidator, IValidator<Register>>(); 
        } 
    } 
    
  3. 更换RegistrationFeature登记在App.Host与我们刚创建的新CustomRegistrationFeature。

    Plugins.Add(new CustomRegistrationFeature()); 
    

我不知道为什么这工作,因为我只是在做相同或类似的事情什么已经存在,但它确实。它也允许我添加更多的验证规则(这就是为什么我需要这样做)。

1

我不太确定这是一个错误,但它可能是。无论如何,不​​使用自定义验证程序的原因在于,在运行Configure()方法之后才会调用Register()方法RegistrationFeature,因此将覆盖CustomRegistrationValidator的注册。

最简单的办法是注册自定义验证Register()方法已运行:

public override void OnAfterInit() 
{ 
    base.OnAfterInit(); 

    RegisterAs<CustomRegistrationValidator, IValidator<Register>>();    
}