示例#1
0
        public static void AuthorizeForRole(string role)
        {
            var pctx = ContextRegistry.ContextsOf("Principal").FirstOrDefault();

            if (null == pctx)
            {
                throw new AuthenticationException("No authenticated identity");
            }

            var id = pctx.Segments.First();

            ApplicationPrincipal pr;

            if (!_cachedPrincipals.MaybeGetItem(id, out pr))
            {
                using (
                    var dc =
                        DocumentStoreLocator.ResolveOrRoot(
                            ContextualAuthorizationConfiguration.PrincipalsStore))
                {
                    pr = dc.Load <ApplicationPrincipal>(id);
                    _cachedPrincipals.Add(id, pr);
                }
            }

            if (!pr.IsInRole(role))
            {
                throw new SecurityException(string.Format("principal {0} is not in role {1}", id, role));
            }
        }
        public byte[] DownloadData(string file, int startRange, int length)
        {
            Debug.Assert(!string.IsNullOrWhiteSpace(file));
            Debug.Assert(startRange >= 0);
            Debug.Assert(length < _memoryHog);

            Stream data;

            if (!_cache.MaybeGetItem(file, out data))
            {
                _log.InfoFormat("Downloading {0} from blob storage and caching", file);

                var blobs = Client.FromConfig().ForBlobs();
                var c     = blobs.GetContainerReference(_container);
                var b     = c.GetBlobReference(file);
                using (MemoryStream ms = new MemoryStream())
                {
                    b.DownloadToStream(ms);
                    _cache.Add(file, ms);
                    data = ms;
                }
            }
            else
            {
                _dblog.InfoFormat("Cache hit for file download {0}", file);
            }

            if (startRange >= data.Length)
            {
                return(null);
            }

            byte[] retval = new byte[length];
            data.Seek(startRange, SeekOrigin.Begin);
            int read = data.Read(retval, 0, length);

            if (read < length)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(retval, 0, read);
                    retval = ms.ToArray();
                }
            }

            return(retval);
        }
        public byte[] RetrieveKey(string key, bool fromCache = true)
        {
            byte[] retval = null;

            bool found = false;

            if (fromCache)
            {
                _dblog.InfoFormat("Found image {0} in cache", key);
                lock (_cache)
                {
                    found = _cache.MaybeGetItem(key, out retval);
                }
            }

            if (!found)
            {
                _log.InfoFormat("Retrieving image {0} from blob storage into cache", key);
                return(RetrieveFromBlobStorage(key));
            }

            return(retval);
        }