Skip to content

Implementing Audit Trails Using ASP.NET MVC ActionFilters

License

Notifications You must be signed in to change notification settings

kadirogreten/AuditTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Audit Table For Asp.Net MVC

Implementing Audit Trails using ASP.NET MVC ActionFilters

The audit table shows which IP address the people have entered into the website, which user name they entered, which browser they used, which pages were clicked on the website.

You can contact me from our website.

https://www.weezstudio.com

1- Create a Audit Model

public class Audit
{
    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; }
    public string Browser { get; set; }

    // Default Constructor
    public Audit() { }



}


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,
            Browser = request.Browser.Browser,

            // The URL that was accessed
            AreaAccessed = request.RawUrl,
            // Creates our Timestamp
            Timestamp = DateTime.Now
        };

        // Stores the Audit in the Database
        ApplicationDbContext context = new ApplicationDbContext();
        context.AuditRecords.Add(audit);
        context.SaveChanges();

        // Finishes executing the Action as normal 
        base.OnActionExecuting(filterContext);
    }
}

2 - Put the attribute in the homecontroller

[Audit]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

About

Implementing Audit Trails Using ASP.NET MVC ActionFilters

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published