OnActionExecuting
: just before the action method is called.
OnResultExecuted
: occurs after the result is executed. : after the view is rendered.
public class AuthActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpCookie idCookie = filterContext.HttpContext.Request.Cookies.Get("id"); HttpCookie passwordCookie = filterContext.HttpContext.Request.Cookies.Get("password"); if ( filterContext.HttpContext.Session[ "Auth" ] == null || ( bool )filterContext.HttpContext.Session[ "Auth" ] == false ) { if ( idCookie != null && passwordCookie != null ) { HostwayMembership membership = new HostwayMembership(); if ( membership.ValidateUser(idCookie.Value, passwordCookie.Value) ) { filterContext.HttpContext.Session[ "Auth" ] = true; string authUserName = membership.GetUserName(idCookie.Value, passwordCookie.Value); filterContext.HttpContext.Session["UserName"] = authUserName; using(SYNCmailCSDatabase csDB = new SYNCmailCSDatabase()) { csDB.CreateCSEvent(authUserName, com.hostway.lib.SYNCmail.Database.CSEventCase.CSUserLogin, authUserName, "Logon", "Logon user"); } } } } if ( filterContext.HttpContext.Session[ "Auth" ] == null || (bool) filterContext.HttpContext.Session[ "Auth" ] == false) { filterContext.Cancel = true; filterContext.HttpContext.Response.Redirect("~/Logon/Index"); } else { string userName = (string) filterContext.HttpContext.Session[ "UserName" ]; string actionMethodName = filterContext.ActionMethod.Name; using(SYNCmailCSDatabase csDB = new SYNCmailCSDatabase()) { string comment = string.Format("{0} user action method : {1}", userName, actionMethodName); csDB.CreateCSEvent(userName, CSEventCase.EnterPage, actionMethodName, filterContext.HttpContext.User.Identity.Name, comment); } base.OnActionExecuting(filterContext); } } }