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 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