Skip to main content
All docs
V23.1

Audit the Current User or Host Identity (XPO)

  • 2 minutes to read

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 demonstrates how this can be implemented.

XAF Windows Forms Application - Program.cs (Program.vb) file:

using System.Security.Principal;
//...
static void Main() {
    MySolutionWindowsFormsApplication application = new MySolutionWindowsFormsApplication();
    // ...
    //Subscribe to QueryCurrentUserName event of the AuditTrailService's 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;
}

XAF ASP.NET Web Forms Application - Global.asax.cs (Global.asax.vb) file:

protected void Session_Start(object sender, EventArgs e) {
    WebApplication.SetInstance(Session, new MySolutionWebApplication());
    AuditTrailService.Instance.QueryCurrentUserName += 
        new QueryCurrentUserNameEventHandler(Instance_QueryCurrentUserName);
    //...
    WebApplication.Instance.Setup();
    WebApplication.Instance.Start();
}
private void Instance_QueryCurrentUserName(object sender, QueryCurrentUserNameEventArgs e) {
    e.CurrentUserName = 
        String.Format("Web user ({0})", HttpContext.Current.Request.UserHostAddress);
}