Accountability is always important, especially when dealing with systems that contain sensitive data. It can often be helpful to have an audit trail that will allow you to go back and retrace the steps that a user took, what they saw, where they went and ultimately what they may have done wrong.
This post will cover how to implement a very basic Audit trail within your ASP.NET MVC Application using custom ActionFilters, which will allow you to decorate specific Controllers and Actions that will be designated to log details of a specific user interaction to later identify (or incriminate) them if necessary.
The Problem
A specific user within your system continues to submit incorrect, misspelled and erroneous data in one of the areas of your sensitive application, however you have no current means of figuring out exactly who they are.
The Solution
We can solve this through creating a custom ActionFilter that we can use to decorate specific Controllers and Actions to be flagged as Auditable and will insert an Audit record into our Auditing database or table. First, we will create a very basic framework for a custom ActionFilter.
- public class AuditAttribute: ActionFilterAttribute
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- // Additional Auditing-based Logic Here
- base.OnActionExecuting(filterContext);
- }
- }
Then we will need to determine what kinds of information that we will want to store within each of our Audit Records, so we will create an Audit class to store the values and a Context that can be used to access it. Our class will initially consist of the following fields that will constitute a single Audit record:
- AuditID - A unique identifier for this specific Audit instance.
- UserName - The UserName of the user performing the Action (if available).
- IPAddress - The IP Address of the user performing the Action.
- AreaAccessed - This will store the URL of the area that was accessed.
- Timestamp - A Timestamp of when the User accessed the area.
Which might look like:
- // Example Audit Class
- public class Audit
- {
- // Audit Properties
- public Guid AuditID
- {
- get;
- set;
- }
- public string UserName
- {
- get;
- set;
- }
- public string IPAddress
- {
- get;
- set;
- }
- public string AreaAccessed
- {
- get;
- set;
- }
- public DateTime Timestamp
- {
- get;
- set;
- }
- // Default Constructor
- public Audit()
- {}
- }
- // Example AuditingContext
- public class AuditingContext: DbContext
- {
- public DbSet < Audit > AuditRecords
- {
- get;
- set;
- }
- }
- <connectionStrings>
- <!-- Default Connection goes here -->
- <!-- Our Auditing Context -->
- <add name="AuditContext"
- connectionString="Data Source=(LocalDB)v11.0;AttachDbFilename=|DataDirectory|Auditing.mdf;Integrated Security=True"
- providerName="System.Data.SqlClient"
- />
- </connectionStrings>
Now that we have a very basic method of saving and retrieving Audit records, we can start fleshing out our custom ActionFilter to populate an Audit whenever a decorated action is performed.
- public class AuditAttribute: ActionFilterAttribute
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- // Stores the Request in an Accessible object
- var request = filterContext.HttpContext.Request;
- // Generate an audit
- Audit audit = new Audit()
- {
- // Your Audit Identifier
- AuditID = Guid.NewGuid(),
- // Our Username (if available)
- UserName = (request.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : "Anonymous",
- // The IP Address of the Request
- IPAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ? ? request.UserHostAddress,
- // The URL that was accessed
- AreaAccessed = request.RawUrl,
- // Creates our Timestamp
- TimeAccessed = DateTime.UtcNow
- };
- // Stores the Audit in the Database
- AuditingContext context = new AuditingContext();
- context.AuditRecords.Add(audit);
- context.SaveChanges();
- // Finishes executing the Action as normal
- base.OnActionExecuting(filterContext);
- }
- }
- [Authorize]
- public class HomeController: Controller
- {
- public ActionResult NormalAction()
- {
- return View();
- }
- [Audit]
- public ActionResult AuditedAction()
- {
- return Content("Audit Fired!");
- }
- // Other Actions omitted for brevity
- }
So after creating as many auditable actions as you desire, you can log in to your application and begin hitting as many of the [Audit] actions as you can to build a decent set of them within your Audit database. When you are all finished with this, we will create a very basic action to display our audit trail:
- public ActionResult ViewAuditRecords()
- {
- var audits = new AuditingContext().AuditRecords;
- return View(audits);
- }

Additional Functionality Coming Soon
This blog post covered establishing a very basic system for setting up auditing that could easily be expanded upon. A follow-up will be posted in the future and will discuss methods of adding additional functionality to the Auditing ActionFilter such as serializing Request Data using JSON (to see exactly what our users were posting into fields), creating relationships within Audit objects (to monitor User Sessions) and more!
You can download this current example to update, modify and change to suit your needs as well from the following link:
- Download this Basic Auditing Example from Github


Ekrem TapanPosted Oct 28, 2015, 11:32 PM EDT
good article
Nilesh JadavPosted Oct 28, 2015, 6:21 PM EDT
Good Work sir !
Vivek TripathiPosted Oct 28, 2015, 9:07 AM EDT
nice article
Ravi PatelPosted Oct 28, 2015, 7:11 AM EDT
excellent article sir thanks a lot
Mohammed IbrahimPosted Oct 28, 2015, 5:26 AM EDT
nice
Sibeesh VenuTOP 100Posted Oct 28, 2015, 4:40 AM EDT
Nice Share
Nilesh JadavPosted Oct 28, 2015, 4:29 AM EDT
GOod Share !1