File size: 1,565 Bytes
e26fba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;
using System.Net.Http;
using System.Web.Http;
using MovieTube.Web.Repository;
using MovieTube.Web.Services;

namespace MovieTube.Web.Attributes
{
    public class UserProfileFilterAttribute : ActionFilterAttribute
    {
        private readonly IUserProfileService userProfileService;
        private readonly ICookieService cookieService;

        public UserProfileFilterAttribute()
        {
            this.userProfileService = new UserProfileService();
            this.cookieService = new CookieService();
        }

        public override void  OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var cookie = actionContext.Request.Headers.GetCookies(Constants.CookieSessionName).FirstOrDefault();

            var profile = userProfileService.UpdateVisitorProfile(
                    cookieService.DecryptCookie(cookie != null ? cookie[Constants.CookieSessionName].Value : null),
                    actionContext.Request.GetClientIpAddress(), actionContext.Request.RequestUri.PathAndQuery,
                    actionContext.Request.GetQueryString("lang"));
               if(cookie == null)
                   actionContext.Response.SetCookie(Constants.CookieSessionName,
                       cookieService.EncryptCookie(profile.ID), DateTime.Now.AddYears(100));

 	         base.OnActionExecuting(actionContext);
        }

        

    }
}