【发布时间】:2011-01-18 21:49:30
【问题描述】:
我有一个有效的自定义 UserNamePasswordValidator 调用我的 Oracle DB。
该类派生自 System.IdentityModel.Selectors.UserNamePasswordValidator,Validate() 方法返回 void。
我从数据库加载我的用户对象,一旦密码被验证,我想隐藏我的“用户”对象,以便服务在开展业务时可以访问它。在 ASP.NET / Java 领域,我会将其存储到会话中,或者可能是我的整个控制器类。如何从 WCF 中的验证器执行此操作?
或者,换句话说,在 WCF 领域中为服务设置自定义用户域对象的最佳实践是什么。
更新:这就是我解决它的方法。我在验证期间缓存了 User 对象,然后在 AuthorizatinPolicy 步骤中访问它。
// this gets called after the custom authentication step where we loaded the User
public bool Evaluate(EvaluationContext evaluationContext, ref object state)
{
// get the authenticated client identity
IIdentity client = GetClientIdentity(evaluationContext);
User user;
OraclePasswordValidator.users.TryGetValue(client.Name, out user);
if(user != null) {
// set the custom principal
evaluationContext.Properties["Principal"] = user;
return true;
}
return false;
}
【问题讨论】:
-
我现在将我的用户对象 (IPrincipal) 加载到密码验证器中,将其缓存在静态字典中,并在 AuthoriziationPolicy 中获取它。请参阅上面的编辑。这是最好的方法吗?对于这样一个丰富的框架来说,这似乎是一个拼凑。
-
好吧,我在上面的编辑中搞砸了格式,无法修复。那应该是上面代码区的完整方法了。
标签: c# wcf wcf-security