잊지 않겠습니다.

Action Filter

.NET Framework 2009. 1. 7. 12:59

OnActionExecuting

 : just before the action method is called.

OnActionExecuted

 : just after the action method is called.

OnResultExecuting

 : occurs just before the result is executed. : before view is rendered.

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);
        }
    }
}

 

 

 

 


Posted by Y2K
,