How to register the current user using NHibernate Envers

I looked at the way to register the username of the current user in the Envers revision entity table. However, the little information and information that I managed to find was not so useful.

I have a revision object and a listener that 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 in an ASP.NET environment. Configuration and factory sessions are created once, as this is an expensive operation. A new session is opened for each transaction.

From this, the NHE Jira problem and this related SO question understand that when setting up nHibernate, you can register the username while listening.

However, this is not very good for me, since it will register the username of the one who connects to the application first for each transaction.

I need to specify the username associated with a particular transaction to the listener.

I used to use a poor person, checking the use of event listeners before and after updating nHibernate. I ran into a similar problem here , as these listeners are also associated with the factory session, not the session. So I switched to old-style interceptors that might be associated with a session inside OpenSession . This allowed me to add the current user to the interceptor.

However, I do not know how to solve the same problem for Envers. Hope someone can help.

+4
source share
1 answer

You can use the Thread.CurrentPrinicpal property ( MSDN ). ASP.NET Membership Mechanism sets it to the identifier associated with the current registered user

+2
source

Source: https://habr.com/ru/post/1389233/


All Articles