Ejemplo n.º 1
0
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind(typeof(IUnitOfWork)).ToMethod <IUnitOfWork>(context =>
            {
                SellItDbContext databaseContext = new SellItDbContext();
                return(new UnitOfWork(databaseContext));
            }).InRequestScope();

            kernel.Bind <IUserService>().To <UserService>().InRequestScope();
            kernel.Bind <IAdvertisementService>().To <AdvertisementService>().InRequestScope();

            System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
        }
Ejemplo n.º 2
0
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            AuthenticationHeaderValue header = actionContext.Request.Headers.Authorization;

            if (header == null || header.Parameter == "null")
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                return;
            }

            JwtSecurityTokenHandler jwtHandler = new JwtSecurityTokenHandler();
            JwtSecurityToken        token      = jwtHandler.ReadToken(header.Parameter) as JwtSecurityToken;

            if (token == null || token.ValidTo < DateTime.Now)
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                return;
            }

            Claim tokenClaim = token.Claims.FirstOrDefault();

            if (tokenClaim == null)
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                return;
            }

            SellItDbContext context = new SellItDbContext();

            CurrentUser currentUser = context.Users
                                      .Where(x => x.Uid == new Guid(tokenClaim.Value))
                                      .Select(s => new CurrentUser
            {
                Id   = s.Id,
                Uid  = s.Uid,
                Role = s.Role
            }).FirstOrDefault();

            if (currentUser == null || (!string.IsNullOrEmpty(Roles) && !Roles.Contains(((UserRole)currentUser.Role).ToString())))
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                return;
            }

            CacheItemPolicy policy = new CacheItemPolicy
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(15)
            };

            MemoryCache.Default.Set("currentUser", currentUser, policy);
        }