Skip to content

Instantly share code, notes, and snippets.

@rionmonster
Created December 2, 2016 20:22
Show Gist options
  • Save rionmonster/240cbe44b6875f179e9687606efe6782 to your computer and use it in GitHub Desktop.
Save rionmonster/240cbe44b6875f179e9687606efe6782 to your computer and use it in GitHub Desktop.
Example with Possible NHibernate Implementation
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
};
// Create your NHibernate session / repository here and
// save your audit object
//Finishes executing the Action as normal
base.OnActionExecuting(filterContext);
}
}
@mefrachi
Copy link

mefrachi commented Dec 8, 2016

@rionmonster Hey thanks so much for this, it worked, the info is stored into the db.
But now im having a problem to present the actual information from the db table to the view.

i am trying to show it with a list view.

@model IEnumerable<Dominio.Entidades.Audit>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IPAddress)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AreaAccessed)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Timestamp)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IPAddress)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AreaAccessed)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Timestamp)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.AuditID }) |
            @Html.ActionLink("Details", "Details", new { id=item.AuditID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.AuditID })
        </td>
    </tr>
}

</table>
`

But it is giving me an exception that the model is null.
I made the audit class and mapped it with NH and the model im bringing it from that audit class.

@mefrachi
Copy link

mefrachi commented Dec 8, 2016

@rionmonster nevermind i solved that problem, but sadly im now stuck on the second part(advanced audit) part of your project.
Im stuck at
var sessionIdentifier = string.Join("", MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(request.Cookies[FormsAuthentication.FormsCookieName].Value)).Select(s => s.ToString("x2")));
Where it always gives me the exception:
Object reference not set to an instance of an object

Any ideas please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment