gpt4 book ai didi

servicestack - 如何覆盖 ServiceStack RegistrationService Validator?

转载 作者:行者123 更新时间:2023-12-01 01:04:44 30 4
gpt4 key购买 nike

如何覆盖 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com