Skip to main content

Audit the Current User or Host Identity (XPO)

If you build a business application that is intended for use by several end users at the same time, you need to get information on who made a particular change. For this purpose, the CurrentUserName property of the application’s Security System is passed to the Audit Trail system. If you do not use the XAF Security System, handle the QueryCurrentUserName event to specify the user name for audit records. In an XAF Windows Forms application, you can get the user name from the WindowsIdentity object. In an XAF ASP.NET Web Forms application, you can use the value returned by the HttpContext.Current.Request.UserHostAddress property. The following code sample demonstrates how this can be implemented.

File: MySolution.Win\Program.cs, MySolution.Web\Global.asax.cs

using System.Security.Principal;
// ...
static void Main() {
    MySolutionWindowsFormsApplication application = new MySolutionWindowsFormsApplication();
    // ...
    // Subscribe to the QueryCurrentUserName event of the AuditTrailService instance
    AuditTrailService.Instance.QueryCurrentUserName += 
       new QueryCurrentUserNameEventHandler(Instance_QueryCurrentUserName);
    application.Setup();
    application.Start();
    // ...
}
static void Instance_QueryCurrentUserName(object sender, QueryCurrentUserNameEventArgs e) {
    e.CurrentUserName = WindowsIdentity.GetCurrent().Name;
}