Try using below code.
UPDATE:
For asp.net web api, please try this
public class AuditAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var context = actionContext.RequestContext;
var user = context.Principal.Identity.IsAuthenticated ? context.Principal.Identity.Name : string.Empty;
var ip = GetClientIpAddress(actionContext.Request);
var action = actionContext.ActionDescriptor.ActionName;
var controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
base.OnActionExecuting(actionContext);
}
private string GetClientIpAddress(HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return IPAddress.Parse(((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress).ToString();
}
if (request.Properties.ContainsKey("MS_OwinContext"))
{
return IPAddress.Parse(((OwinContext)request.Properties["MS_OwinContext"]).Request.RemoteIpAddress).ToString();
}
return String.Empty;
}
}
And for asp.net MVC, you can try this
public class AuditAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// get user name + ip address + controlleraction
var controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var action = filterContext.ActionDescriptor.ActionName;
var ip = filterContext.HttpContext.Request.UserHostAddress;
var dateTime = filterContext.HttpContext.Timestamp;
var user = GetUserName(filterContext.HttpContext);
}
private string GetUserName(HttpContext httpContext)
{
var userName = string.Empty;
var context = httpContext.Current;
if (context != null && context.User != null && context.User.Identity.IsAuthenticated)
{
userName = context.User.Identity.Name;
}
else
{
var threadPincipal = Thread.CurrentPrincipal;
if (threadPincipal != null && threadPincipal.Identity.IsAuthenticated)
{
userName = threadPincipal.Identity.Name;
}
}
return userName;
}
}
Update 2 : Retrieving Client IP address is always a tricky business because there are lot of factors that has to be considered. How are clients accessing the application? Are they coming thru a proxy server? IP addresses can be spoofed, so there is no 100% reliable way. Looking at the Http Headers will provide you some level of success in both web api and mvc. But you always have to consider the fact that there will be cases where client IP is not valid.
How can I get the client's IP address in ASP.NET MVC?