protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            // Write your Authentication code here
            IEnumerable<string> monsterApiKeyHeaderValues = null;

            // Checking the Header values
            if (request.Headers.TryGetValues("X-ViewerAppApiKey", out monsterApiKeyHeaderValues))
            {
                var apiKeyHeaderValue = monsterApiKeyHeaderValues.First().Split(':');
                var appID = apiKeyHeaderValue[0];
                var appKey = apiKeyHeaderValue[1];
                var userName = apiKeyHeaderValue[2];

                var applicationKey = new ApplicationKey
                        {
                            AppId = new Guid(appID),
                            Key = new Guid(appKey),
                            IsDeleted = false
                        };
                ApplicationKeyService = new ApplicationKeyService(); //TODO : user DI to replace direct instantiation
                var dbApplicationKeyModel = ApplicationKeyService.GetApplicationKey(applicationKey);

                if (null == dbApplicationKeyModel) return requestCancel(request, cancellationToken, Constants.SlidingExpirationToken);

                if (appID.Equals(dbApplicationKeyModel.AppId.ToString()) && appKey.Equals(dbApplicationKeyModel.Key.ToString()) && userName.ToUpper().Equals(dbApplicationKeyModel.UserName.ToUpper()))
                {
                    var userNameClaim = new Claim(ClaimTypes.Name, appID);
                    var identity = new ClaimsIdentity(new[] { userNameClaim }, "MonsterAppApiKey");
                    identity.AddClaim(new Claim("AppKey", appKey));
                    identity.AddClaim(new Claim("AppId", appID));

                    var principal = new ClaimsPrincipal(identity);
                    Thread.CurrentPrincipal = principal;

                    if (HttpContext.Current != null) HttpContext.Current.User = principal;
                }
                else
                    return requestCancel(request, cancellationToken, Constants.InvalidToken);
            }
            else if (request.Headers.TryGetValues("X-ForumSummaryKey", out monsterApiKeyHeaderValues))
            {
                var userNameClaim = new Claim(ClaimTypes.Name, "");
                var identity = new ClaimsIdentity(new[] { userNameClaim }, "MonsterAppApiKey");
                identity.AddClaim(new Claim("AppKey", ""));
                identity.AddClaim(new Claim("AppId", ""));

                var principal = new ClaimsPrincipal(identity);
                Thread.CurrentPrincipal = principal;

                if (HttpContext.Current != null) HttpContext.Current.User = principal;
            }
            else
                return requestCancel(request, cancellationToken, Constants.MissingToken);

            return base.SendAsync(request, cancellationToken);
        }
Exemplo n.º 2
0
        public HttpResponseMessage GetImage(int nextPrevCount)
        {
            ApplicationKeyService = new ApplicationKeyService(); //TODO : user DI to replace direct instantiation
            var identity = (ClaimsIdentity)HttpContext.Current.User.Identity;
            IEnumerable<Claim> claims = identity.Claims;

            var appKey = new ApplicationKey
                         {
                             AppId = new Guid(claims.First(x => x.Type == "AppId").Value),
                             Key = new Guid(claims.First(x => x.Type == "AppKey").Value),
                             IsDeleted = false
                         };

            var dbApplicationKeyModel = ApplicationKeyService.GetApplicationKey(appKey);

            if (dbApplicationKeyModel.LastUpdatedDate.Date == DateTime.Now.Date &&
                dbApplicationKeyModel.LastUpdatedDate.TimeOfDay.Add(new TimeSpan(0, 0, ApplicationConfiguration.DocumentExpirationMinute.Value, 0)) >= DateTime.Now.TimeOfDay)
            {
                var localPath = AppDomain.CurrentDomain.BaseDirectory + ApplicationConfiguration.RemoveDirectoryNFileFromServer + Path.DirectorySeparatorChar + dbApplicationKeyModel.Key + Path.DirectorySeparatorChar + "Water_" + dbApplicationKeyModel.Key + nextPrevCount + ApplicationConfiguration.ImageExtension;
                //var localPath = string.Empty;
                //if(nextPrevCount%2==0)
                //    localPath = AppDomain.CurrentDomain.BaseDirectory + "/Content/Img/Systems-Mac-Os-icon.png";
                //else
                //    localPath = AppDomain.CurrentDomain.BaseDirectory + "/Content/Img/Systems-Windows-8-icon.png";
                var fileInfo = new FileInfo(localPath);
                byte[] data;
                if (File.Exists(localPath))
                {
                    data = new byte[fileInfo.Length];
                    using (var fs = fileInfo.OpenRead())
                    {
                        fs.Read(data, 0, data.Length);
                    }

                    dbApplicationKeyModel.IsDeleted = false;
                    dbApplicationKeyModel.LastUpdatedDate = DateTime.Now;
                    dbApplicationKeyModel.LastUpdatedBy = dbApplicationKeyModel.UserName;
                    ApplicationKeyService.UpdateApplicationKey(dbApplicationKeyModel);
                }
                else
                {
                    dbApplicationKeyModel.IsDeleted = false;
                    dbApplicationKeyModel.CurrentPage = nextPrevCount;
                    dbApplicationKeyModel.LastUpdatedDate = DateTime.Now;
                    dbApplicationKeyModel.LastUpdatedBy = dbApplicationKeyModel.UserName;
                    dbApplicationKeyModel.StatusId = 1;
                    ApplicationKeyService.UpdateApplicationKeyCurrentPage(dbApplicationKeyModel);

                    var test = RetryHelper.Do(() => ApplicationKeyService.GetProcessorStatus(dbApplicationKeyModel), TimeSpan.FromSeconds(ApplicationConfiguration.RetryInterval.Value), (int)ProcessorStatus.Completed, ApplicationConfiguration.RetryAttempts.Value);
                    if (test != (int)ProcessorStatus.Completed) throw new Exception("The file you are attempting to download has failed. Please try again to download a new copy of the document.");

                    data = new byte[fileInfo.Length];
                    using (var fs = fileInfo.OpenRead())
                    {
                        fs.Read(data, 0, data.Length);
                    }

                }
                var httpResponseMessage = new HttpResponseMessage();
                var memoryStream = new MemoryStream(data.Encrypt());
                //var memoryStream = new MemoryStream(data);
                httpResponseMessage.Content = new ByteArrayContent(memoryStream.ToArray());
                memoryStream.Dispose();
                return httpResponseMessage;
            }

            if (Directory.Exists(dbApplicationKeyModel.DocumentPath))
                Directory.Delete(dbApplicationKeyModel.DocumentPath, true);

            dbApplicationKeyModel.IsDeleted = true;
            dbApplicationKeyModel.LastUpdatedDate = DateTime.Now;
            dbApplicationKeyModel.LastUpdatedBy = dbApplicationKeyModel.UserName;
            ApplicationKeyService.UpdateApplicationKey(dbApplicationKeyModel);

            return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(Intranet.Common.Constants.SlidingExpirationToken) };
        }