All docs
V20.2
20.2
20.1
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Audit the Current User or Host Identity

  • 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, in an XAF Windows Forms application you can get the user name from the WindowsIdentity object. In an XAF ASP.NET Web application, you can use the value returned by the HttpContext.Current.Request.UserHostAddress property. The following code demonstrates how this can be implemented.

NOTE

If you do not specify the current user for the Audit Trail system, the null value will be logged.

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 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);
}