servicestack - 如何覆盖 ServiceStack RegistrationService Validator?

标签 servicestack

如何覆盖 ServiceStack RegistrationService Validator 并向其添加一些新规则?

以及需要做什么来拦截 UserAuthService 验证?

这是 AppHost 配置:

  Plugins.Add(new CorsFeature()); //Registers global CORS Headers

  RequestFilters.Add((httpReq, httpRes, requestDto) =>
  {
    // Handles Request and closes Responses after emitting global HTTP Headers
    if (httpReq.HttpMethod == "OPTIONS")
      httpRes.EndRequest();
  });

  // Enable the validation feature
  Plugins.Add(new ValidationFeature());

  // This method scans the assembly for validators
  container.RegisterValidators(typeof(AppHost).Assembly);

  container.Register<ICacheClient>(new MemoryCacheClient());

  //var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);
  var dbFactory = new OrmLiteConnectionFactory(connectionString, SqliteDialect.Provider);

  container.Register<IDbConnectionFactory>(dbFactory);

  // Enable Authentication
  Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
            new CustomAuthProvider(), 
        }));

  // Provide service for new users to register so they can login with supplied credentials.
  Plugins.Add(new RegistrationFeature());

  // Override the default registration validation with your own custom implementation
  container.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>();

  container.Register<IUserAuthRepository>(c => new CustomAuthRepository(c.Resolve<IDbConnectionFactory>()));

最佳答案

ServiceStack 验证器非常易于使用。 “SocialBootstrap”示例展示了如何在其 AppHost.cs 中使用自定义验证器进行注册。 .

//Provide extra validation for the registration process
public class CustomRegistrationValidator : RegistrationValidator
{
    public CustomRegistrationValidator()
    {
        RuleSet(ApplyTo.Post, () => {
            RuleFor(x => x.DisplayName).NotEmpty();
        });
    }
}

请记住也要注册您的自定义验证器。
//override the default registration validation with your own custom implementation
container.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>();

使用“RuleSet”添加更多规则。希望有帮助。

编辑
似乎当前 v3 版本的 ServiceStack 中可能存在阻止调用验证器的错误。我对 Social Bootstrap 项目进行了快速测试,可以重现您遇到的情况,例如 CustomRegistrationValidator 不触发其规则。其他验证器似乎工作正常,因此目前不确定原因可能是什么。有空我会拉下源码进行调试。如果您碰巧事先这样做,请张贴您的发现,因为它可能会帮助他人。

更新
这个问题是由于插件和注册的操作顺序造成的。注册插件正在运行 Register CustomRegistrationValidator 后的函数已注册并覆盖注册为 IValidator<Registration> 的类型.

解决此问题的最简单方法是创建您自己的 RegistrationFeature,因为它本身非常简单。
public class MyRegistrationFeature : IPlugin
{
    public string AtRestPath { get; set; }

    public RegistrationFeature()
    {
        this.AtRestPath = "/register";
    }

    public void Register(IAppHost appHost)
    {
        appHost.RegisterService<RegisterService>(AtRestPath);
        appHost.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>();
    }
}

关于servicestack - 如何覆盖 ServiceStack RegistrationService Validator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19802472/

相关文章:

servicestack - 为什么 v3 分支中缺少 ServiceStack.Client 项目?

c# - 当路径中存在句点时找不到 ServiceStack 处理程序

jquery - 使用服务器端异步时,Se​​rviceStack Forbidden (403) 错误作为内部服务器错误 (500) 返回

redis - ServiceStack.Redis.Sentinel 用法

c# - 将参数作为路径段发送在 ServiceStack 中不起作用

c# - Service Stack Redis 连接的线程安全

c# - 异步 ServiceStack Web 服务调用后回调以更新 GUI

c# - 在 ServiceStack 中设置 MaxRecievedMessageSize

c# - ServiceStack:如何处理错误?

c# - 将用户定义类的实例作为数据传递给 ServiceStack PUT