在自定义验证器、服务堆栈 API 中隔离/访问设置的会话信息 [英] Isolating/Accessing set session information within custom validator, servicestack API
问题描述
我想知道从我们通过 servicestack 的 fluent-validation API 挂钩连接的自定义验证器中访问用户 AuthSession 的最佳方式.基本上,需求迫使我们使用名为DocNumberValidator"的类通过此验证器访问数据库.当用户进行身份验证时,用户的 ID 会保存到会话中,接下来我们需要这些信息来完成成功的 SQL 查询.如何访问此会话信息(见下文....).IoC 容器没有对 AuthSession 的引用?
I was wondering the best way of accessing the a user's AuthSession from within a custom validator we have hooked up via the servicestack's fluent-validation API hooks. Basically, the requirements are forcing us to access the database through this validator using a class called "DocNumberValidator ". The user's Id is saved into session when they authenticate, down the line we need this information to complete a successful SQL query. How does one access this session information (see below....). The IoC container does not have reference to the 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;
}
}
推荐答案
不确定这是最好的方法.接受建议.
Not really sure this is the best way to do it. Open to suggestions.
- 使用 SessionFeature.GetSessionKey() 获取会话密钥
- 添加
public ICacheClient CacheClient { get;放;}
到验证器.将被 IoC 容器注入 - 从 AuthUserSession(或您使用的任何类型)获取使用key缓存
- Use SessionFeature.GetSessionKey() to get the session key
- Add
public ICacheClient CacheClient { get; set; }
to the validator. Will be injected by IoC container - Get the AuthUserSession (or whatever type you're using) from the cache using the key
添加到您的示例中
public class DocNumberValidator : IDocNumberValidator
{
public IDbConnectionFactory Db { get; set; }
public ICacheClient CacheClient { get; set; }
public bool Validate(string docNum)
{
var isFound = false;
var sessionKey = SessionFeature.GetSessionKey();
var user = CacheClient.Get<AuthUserSession>(sessionKey); //Use whatever class you stored in the session
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", user.UserAuthId)); // user whatever property you need access to
int cnt = (int) cmd.ExecuteScalar();
if (cnt == 1)
isFound = true;
});
return isFound;
}
}
这篇关于在自定义验证器、服务堆栈 API 中隔离/访问设置的会话信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!