我想知道从我们通过 servicestack 的 fluent-validation API 挂钩连接的自定义验证器中访问用户AuthSession的最佳方式。基本上,要求迫使我们使用名为“ DocNumberValidator ”的类通过该验证器访问数据库。用户的 ID 在他们进行身份验证时保存到会话中,我们需要此信息来完成成功的 SQL 查询。如何访问此会话信息(见下文....)。IoC 容器没有对 AuthSession 的引用?
- 我想问题是,有人如何将必要的会话值传递给 SS 验证框架调用的类?
示例代码:
public class MyValidator : AbstractValidator<Statuses>
{
public IDocNumberValidator DocNumberValidator { get; set; }
public StatusesValidator()
{
RuleFor(s => s.DocNumber)
.Must(docNum => this.DocNumberValidator.Validate(docNum))
.WithMessage("Document Number is invalid.")
.WithErrorCode("S-001");
}
}
public class DocNumberValidator : IDocNumberValidator
{
public IDbConnectionFactory Db { get; set; }
public bool Validate(string docNum)
{
var isFound = false;
this.Db.Run(conn =>
{
var sql = "SELECT COUNT([docNumber]) FROM [TABLE] WHERE [docNumber] = @DocNum AND [UserId] = @UserID";
var cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.Add(new SqlParameter("@DocNum", docNum));
cmd.Parameters.Add(new SqlParameter("@UserID", ????????)); // how does one get access to saved users session here
int cnt = (int) cmd.ExecuteScalar();
if (cnt == 1)
isFound = true;
});
return isFound;
}
}