public void AuthenticateRequest(System.Web.HttpContext ctx)
        {
            if (ctx.Request.RawUrl.ToLower().StartsWith("/images")
                || ctx.Request.RawUrl.ToLower().StartsWith("/content")
                || ctx.Request.RawUrl.ToLower().StartsWith("/scripts")
                || ctx.Request.RawUrl.ToLower().StartsWith("/services")
                || ctx.Request.RawUrl.ToLower().StartsWith("/favicon.ico"))
            {
                return;
            }

            var isNewVisitor = false;
            var visitorId = ctx.GetOrCreateVisitorId(out isNewVisitor);
            var principal = new Models.UserPrincipal(visitorId);
            ctx.User = principal;

            var authCookie = ctx.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie == null)
            {
                return;
            }

            var container = System.Web.Mvc.DependencyResolver.Current;
            var logger = container.GetService<Logging.ILogger>();

            logger.Debug("RequestAuthenticated {0}", ctx.Request.Url);
            // Extraction et decryptage du ticket d'autentification
            FormsAuthenticationTicket authTicket = null;
            try
            {
                authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            if (authTicket == null)
            {
                return;
            }

            // Recuperation du userId
            int userId = 0;

            int.TryParse(authTicket.Name, out userId);

            if (userId > 0)
            {
                // Creation d'une nouvelle identité
                var id = new FormsIdentity(authTicket);

                // On passe l'identité et les roles a l'objet Principal
                principal = new Models.UserPrincipal(id, visitorId);
                var authenticationService = container.GetService<Services.IAccountService>();
                var user = authenticationService.GetUserById(userId);
                if (user != null)
                {
                    if (!user.LastLoginDate.HasValue)
                    {
                        user.LastLoginDate = DateTime.UtcNow;
                    }
                    principal.CurrentUser = user;
                    logger.Debug("User : {0} Logged", principal.CurrentUser.FullName);
                }
                else
                {
                    principal = new Models.UserPrincipal(visitorId);
                    FormsAuthentication.SignOut();
                }
                ctx.User = principal;
            }
        }
        public void Error(System.Web.HttpContext ctx)
        {
            // Code that runs when an unhandled error occurs
            if (ctx.Request.Url.Host.IndexOf("localhost") != -1)
            {
                return;
            }
            var ex = ctx.Server.GetLastError();
            var isNewVisitor = false;
            var visitorId = ctx.GetOrCreateVisitorId(out isNewVisitor);
            ex.Data.Add("Message", ex.Message);
            ex.Data.Add("machineName", Environment.MachineName);
            ex.Data.Add("host", ctx.Request.Url.Host);
            ex.Data.Add("visitorId", visitorId);
            ex.Data.Add("userHostAddress", ctx.Request.UserHostAddress);
            ex.Data.Add("userHostName", ctx.Request.UserHostName);
            ex.Data.Add("url", ctx.Request.RawUrl);
            ex.Data.Add("referer", ctx.Request.UrlReferrer);
            ex.Data.Add("applicationPath", ctx.Request.ApplicationPath);
            ex.Data.Add("user-agent", ctx.Request.Headers["User-Agent"]);
            ex.Data.Add("cookie", ctx.Request.Headers["Cookie"]);
            if (ctx.Request.Form.Count > 0)
            {
                ex.Data.Add("begin-form", "-----------------------");
                foreach (var item in ctx.Request.Form.AllKeys)
                {
                    ex.Data.Add(item, ctx.Request.Form[item]);
                }
                ex.Data.Add("end-form", "-----------------------");
            }
            if (ctx.User.Identity.IsAuthenticated)
            {
                ex.Data.Add("user", ctx.User.GetUserPrincipal().CurrentUser.Login);
            }
            else
            {
                ex.Data.Add("user", "anonymous");
            }

            var logger = System.Web.Mvc.DependencyResolver.Current.GetService<Logging.ILogger>();
            logger.Error(ex);
        }