109 lines
4.7 KiB
C#
109 lines
4.7 KiB
C#
using Microsoft.Practices.EnterpriseLibrary.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Helpers;
|
|
using System.Web.Mvc;
|
|
|
|
namespace GODATA.Models.AuditLog
|
|
{
|
|
/// <summary>
|
|
/// This class contain method for audit in database
|
|
/// </summary>
|
|
public class AuditRepository : ActionFilterAttribute, IExceptionFilter, IActionFilter
|
|
{
|
|
/// <summary>
|
|
/// Our value to handle our AuditingLevel
|
|
/// </summary>
|
|
public int AuditingLevel { get; set; }
|
|
|
|
/// <summary>
|
|
/// This method is executed when user request or perform a task
|
|
/// </summary>
|
|
/// <param name="filterContext">Represent object of ActionExecutingContext contain all details</param>
|
|
public void OnActionExecuting(ActionExecutingContext filterContext)
|
|
{
|
|
var request = filterContext.HttpContext.Request;
|
|
//var sessionIdentifier = string.Join("", MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(request.Cookies[FormsAuthentication.FormsCookieName].Value)).Select(s => s.ToString("x2")));
|
|
AuditModel objAudit = new AuditModel()
|
|
{
|
|
// SessionID = sessionIdentifier,
|
|
AuditID = Guid.NewGuid(),
|
|
IPAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress,
|
|
URLAccessed = request.RawUrl,
|
|
TimeAccessed = DateTime.UtcNow,
|
|
UserName = (request.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : "Anonymous",
|
|
ControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
|
|
MethodName = filterContext.ActionDescriptor.ActionName,
|
|
InputParameters = Json.Encode(filterContext.ActionParameters)
|
|
};
|
|
//InsertAuditLoggingDetail(objAudit);
|
|
}
|
|
|
|
/// <summary>
|
|
/// This method is executed when user requested task performed
|
|
/// </summary>
|
|
/// <param name="filterContext">Represent object of ActionExecutedContext contain all details</param>
|
|
public void OnActionExecuted(ActionExecutedContext filterContext)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// This method is executed when user authentication performed
|
|
/// </summary>
|
|
/// <param name="filterContext">Represent object of ActionExecutingContext contain all details</param>
|
|
protected virtual void OnAuthorization(AuthorizationContext filterContext)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// This method is executed whenever an exception occured
|
|
/// </summary>
|
|
/// <param name="filterContext">Represent object of ActionExecutingContext contain all details</param>
|
|
public void OnException(ExceptionContext filterContext)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
//#region CRUD METHODS
|
|
|
|
///// <summary>
|
|
///// This method is used to insert audit detail in datanbase
|
|
///// </summary>
|
|
///// <param name="objAudit">Represent object of auditmodel class contain complete audit detail</param>
|
|
//public void InsertAuditLoggingDetail(AuditModel objAudit)
|
|
//{
|
|
// try
|
|
// {
|
|
// //string _connStr = ConfigurationManager.ConnectionStrings["AuditServices"].ConnectionString;
|
|
// string _connStr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
|
|
|
|
// MySqlParameter[] mySqlParam = new MySqlParameter[9];
|
|
|
|
// mySqlParam[0] = new MySqlParameter("audit_id", objAudit.AuditID);
|
|
// mySqlParam[1] = new MySqlParameter("controller_name", objAudit.ControllerName);
|
|
// mySqlParam[2] = new MySqlParameter("input_parameters", objAudit.InputParameters);
|
|
// mySqlParam[3] = new MySqlParameter("ip_address", objAudit.IPAddress);
|
|
// mySqlParam[4] = new MySqlParameter("method_name", objAudit.MethodName);
|
|
// mySqlParam[5] = new MySqlParameter("session_id", objAudit.SessionID);
|
|
// mySqlParam[6] = new MySqlParameter("time_accessed", objAudit.TimeAccessed);
|
|
// mySqlParam[7] = new MySqlParameter("url_accessed", objAudit.URLAccessed);
|
|
// mySqlParam[8] = new MySqlParameter("user_name", objAudit.UserName);
|
|
// int result = MySqlDBHelper.ExecuteNonQuery(_connStr, CommandType.StoredProcedure, "sp_insert_audit_detail", mySqlParam);
|
|
|
|
// }
|
|
// catch (Exception Ex)
|
|
// {
|
|
// Logger.Write(Ex.Message + Ex.StackTrace, "AuditRepository - InsertAuditLoggingDetail");
|
|
// throw new Exception(Ex.Message);
|
|
// }
|
|
//}
|
|
|
|
//#endregion
|
|
}
|
|
} |