private static void SetCustomPrincipal(AuthorizationContext filterContext, Guid validationToken)
        {
            if (validationToken == Guid.Empty)
            {
                SignOutAndSetStatusCode(filterContext);
                return;
            }

            var authToken = Task.Run(async() => await AuthTokenCore.GetAsync(validationToken).ConfigureAwait(false)).GetAwaiter().GetResult();

            if (authToken == null)
            {
                SignOutAndSetStatusCode(filterContext);
                return;
            }

            var user = Task.Run(async() => await UserCore.GetAsync(authToken.UserId).ConfigureAwait(false)).GetAwaiter().GetResult();

            if (user == null)
            {
                SignOutAndSetStatusCode(filterContext);
                return;
            }

            var identity = new CustomIdentity(user);

            var newUser = new CustomPrincipal(identity);

            //set the custom principal
            filterContext.HttpContext.User = newUser;
        }
        protected IdentityInjectedController()
        {
            var cookie = IdentityInjector.GetCookie();

            Identity = null;
            if (cookie != null)
            {
                Identity = Task.Run(() => UserCore.GetAsync(cookie)).ConfigureAwait(false).GetAwaiter().GetResult();
            }

            if (Identity == null)
            {
                Identity = CreateNewUser();
            }
        }
        public async Task <IHttpActionResult> Create([FromBody] LetterModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var letter = new Letter
                {
                    UserId  = Identity.Id,
                    Date    = DateTime.Now,
                    Message = model.Message,
                    Title   = model.Title
                };

                letter = await LetterCore.CreateAsync(letter);

                if (letter == null)
                {
                    return(InternalServerError());
                }

                var user = await UserCore.GetAsync(Identity.Id);

                if (user.Email == model.Email)
                {
                    return(Ok(letter));
                }

                user.Email = model.Email;
                var updatedUser = await UserCore.UpdateAsync(user);

                if (updatedUser == null)
                {
                    return(InternalServerError());
                }

                return(Ok(letter));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }