如何使用 NHibernate Envers 记录当前用户
我研究了如何在 Envers 的修订实体表中记录当前用户的用户名。然而,信息很少,而且我设法找到的信息也没有多大帮助。
我有一个修订实体和侦听器,如下所示:
[RevisionEntity(typeof(EnversRevisionListener))]
public class EnversRevisionEntity : DefaultRevisionEntity
{
public virtual string UserName { get; set; }
}
public class EnversRevisionListener : IRevisionListener
{
private string _userName = "unknown";
public EnversRevisionListener(string userName)
: base()
{
_userName = userName;
}
public void NewRevision(object revisionEntity)
{
var casted = revisionEntity as EnversRevisionEntity;
if (casted != null)
{
casted.UserName = _userName;
}
}
}
我想在 ASP.NET 环境中使用这两个。配置和会话工厂创建一次,因为这是一个昂贵的操作。对于每笔交易,都会打开一个新会话。
从这个 NHE Jira 问题与此相关,SO问题 我知道在配置nHibernate时可以使用监听器注册用户名。
然而,这对我来说还不够好,因为这会注册每笔交易中首先连接到应用程序的用户的用户名。
我需要能够向侦听器提供与特定事务相关的用户名。
以前,我使用 nHibernate 的更新前和更新后事件侦听器进行穷人审计。这里我遇到了类似的问题,因为这些侦听器也相关联与会话工厂而不是会话。因此,我切换到旧式拦截器,它可以与 OpenSession 内的会话关联。这允许我将当前用户添加到拦截器中。
但是,我不知道如何为恩弗斯解决同样的问题。我希望有人能提供帮助。
I've looked around how to log the username of the current user in the revision entity table of Envers. However, there's little information and the information I did manage to find wasn't that helpful.
I have a revision entity and listener which looks like this:
[RevisionEntity(typeof(EnversRevisionListener))]
public class EnversRevisionEntity : DefaultRevisionEntity
{
public virtual string UserName { get; set; }
}
public class EnversRevisionListener : IRevisionListener
{
private string _userName = "unknown";
public EnversRevisionListener(string userName)
: base()
{
_userName = userName;
}
public void NewRevision(object revisionEntity)
{
var casted = revisionEntity as EnversRevisionEntity;
if (casted != null)
{
casted.UserName = _userName;
}
}
}
I want to use these two inside an ASP.NET environment. The configuration and sessions factory are created once as that is an expensive action. For each transaction, a new session is opened.
From this NHE Jira issue and this, related, SO question I understand that it is possible to register the username with the listener when configuring nHibernate.
However, that's not good enough for me as that would register the username of whoever connects to the application first for each and every transaction.
I need to be able to provide the username related to a specific transaction to the listener.
Previously, I used poor man's auditing using nHibernate's pre- and post-update event listeners. Here I faced a similar problem as these listeners are also associated with the session factory and not the session. Therefore I switched to old style interceptors, which can be associated with the session inside OpenSession
. This allowed me to add the current user to the interceptor.
However, I don't know how to solve the same problem for Envers. I hope someone can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Thread.CurrentPrinicpal 属性 (MSDN )。 ASP.NET 成员资格机制将其设置为与当前登录用户关联的身份
You can use Thread.CurrentPrinicpal property (MSDN). ASP.NET membership mechanism sets it to identity associated with currently logged user