Example #1
0
        private void Handle404Error()
        {
            var context    = FrapidHttpContext.GetCurrent();
            int statusCode = context.Response.StatusCode;

            if (statusCode != 404)
            {
                return;
            }

            context.Server.ClearError();
            context.Response.TrySkipIisCustomErrors = true;
            string path = context.Request.Url.AbsolutePath;

            var ignoredPaths = new[]
            {
                "/api",
                "/dashboard",
                "/content-not-found"
            };

            if (!ignoredPaths.Any(x => path.StartsWith(x)))
            {
                context.Server.TransferRequest("/content-not-found?path=" + path, true);
            }
        }
Example #2
0
        internal static string ToCdnResource(string path)
        {
            if (!path.StartsWith("/") ||
                path.StartsWith("//") ||
                path.ToLowerInvariant().StartsWith("/signalr"))
            {
                return(path);
            }

            var approved = new ApprovedDomainSerializer();
            var tenant   = approved.Get().FirstOrDefault(x => x.GetSubtenants().Contains(TenantConvention.GetDomain()));

            if (tenant == null)
            {
                return(path);
            }

            if (!string.IsNullOrWhiteSpace(tenant.CdnDomain))
            {
                var uri = FrapidHttpContext.GetCurrent().Request.Url;

                return(uri.Scheme + Uri.SchemeDelimiter + tenant.CdnDomain + (uri.IsDefaultPort ? "" : ":" + uri.Port) + path);
            }

            return(path);
        }
Example #3
0
        public void App_BeginRequest(object sender, EventArgs e)
        {
            var context = FrapidHttpContext.GetCurrent();

            if (context == null)
            {
                return;
            }

            string domain = TenantConvention.GetDomain();

            Log.Verbose(
                $"Got a {context.Request.HttpMethod} request {context.Request.AppRelativeCurrentExecutionFilePath} on domain {domain}.");

            bool enforceSsl = TenantConvention.EnforceSsl(domain);

            if (!enforceSsl)
            {
                Log.Verbose($"SSL was not enforced on domain {domain}.");
                return;
            }

            if (context.Request.Url.Scheme == "https")
            {
                context.Response.AddHeader("Strict-Transport-Security", "max-age=31536000");
            }
            else if (context.Request.Url.Scheme == "http")
            {
                string path = "https://" + context.Request.Url.Host + context.Request.Url.PathAndQuery;
                context.Response.Status = "301 Moved Permanently";
                context.Response.AddHeader("Location", path);
            }
        }
Example #4
0
        private void App_Error(object sender, EventArgs e)
        {
            var context   = FrapidHttpContext.GetCurrent();
            var exception = context.Server.GetLastError();

            if (exception != null)
            {
                Log.Error("Exception. {exception}", exception);
            }
        }
Example #5
0
        /// <summary>
        /// This investigates and serves static resources present in the tenant's wwwroot folder.
        /// </summary>
        private void ServeRequestAsTenantResource()
        {
            string tenant = TenantConvention.GetTenant();
            string file   = TenantStaticContentHelper.GetFile(tenant, FrapidHttpContext.GetCurrent());

            if (!string.IsNullOrWhiteSpace(file))
            {
                //We found the requested file on the tenant's "wwwroot" directory.
                FrapidHttpContext.GetCurrent().RewritePath(file);
            }
        }
Example #6
0
        private void App_PostAuthenticateRequest(object sender, EventArgs eventArgs)
        {
            string tenant = TenantConvention.GetTenant();
            string file   = TenantStaticContentHelper.GetFile(tenant, FrapidHttpContext.GetCurrent());

            if (!string.IsNullOrWhiteSpace(file))
            {
                //We found the requested file on the tenant's "wwwroot" directory.
                FrapidHttpContext.GetCurrent().RewritePath(file);
            }
        }
Example #7
0
        private void App_Error(object sender, EventArgs e)
        {
            var context   = FrapidHttpContext.GetCurrent();
            var exception = context.Server.GetLastError();

            if (exception == null)
            {
                return;
            }

            this.LogException(exception);
        }
Example #8
0
        public static string GetTenant(string url = "")
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                url = FrapidHttpContext.GetCurrent().Request.Url.Authority;
            }

            var    locator       = GetTenantLocator();
            string defaultTenant = GetDefaultTenantName();

            return(locator.FromUrl(url, defaultTenant));
        }
Example #9
0
        public static string GetDomain()
        {
            if (FrapidHttpContext.GetCurrent() == null)
            {
                return(string.Empty);
            }

            string url       = FrapidHttpContext.GetCurrent().Request.Url.Authority;
            var    extractor = new DomainNameExtractor(Log.Logger);

            return(extractor.GetDomain(url));
        }
        protected override bool AuthorizeCore(HttpContextBase context)
        {
            string tenant     = TenantConvention.GetTenant();
            bool   authorized = AuthorizationManager.IsAuthorizedAsync(tenant, context).Result;

            if (!authorized)
            {
                FrapidHttpContext.GetCurrent().User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
            }

            return(authorized);
        }
Example #11
0
        public async Task <ActionResult> IndexAsync(string categoryAlias = "", int pageNumber = 1)
        {
            if (pageNumber <= 0)
            {
                pageNumber = 1;
            }

            var channel = await RssModel.GetRssChannelAsync(this.Tenant, FrapidHttpContext.GetCurrent(), categoryAlias, pageNumber).ConfigureAwait(false);

            string rss = RssWriter.Write(channel);

            return(this.Content(rss, "text/xml", Encoding.UTF8));
        }
Example #12
0
        public async Task UnInstallAsync()
        {
            var context = FrapidHttpContext.GetCurrent();

            if (context != null)
            {
                throw new UninstallException("Access is denied. Deleting a website is not allowed.");
            }

            await this.CleanupDbAsync().ConfigureAwait(false);

            this.CleanupTenantDirectory();
            new ApprovedDomainSerializer().Remove(this.Url);
        }
Example #13
0
        public static RemoteUser Get(HttpContextBase context = null)
        {
            if (context == null)
            {
                context = new HttpContextWrapper(FrapidHttpContext.GetCurrent());
            }

            return(new RemoteUser
            {
                Browser = context.Request.Browser.Browser,
                IpAddress = context.GetClientIpAddress(),
                Culture = CultureManager.GetCurrent().Name,
                UserAgent = context.Request.UserAgent,
                Country = context.Request.ServerVariables["HTTP_CF_IPCOUNTRY"]
            });
        }
Example #14
0
        public static async Task <LoginView> GetCurrentAsync(string tenant = "")
        {
            var context = FrapidHttpContext.GetCurrent();

            if (string.IsNullOrWhiteSpace(tenant))
            {
                tenant = GetTenant();
            }

            long loginId = 0;

            if (context.User != null)
            {
                long.TryParse(context.User.Identity.Name, out loginId);
            }

            return(await GetCurrentAsync(tenant, loginId).ConfigureAwait(false));
        }
Example #15
0
        private void SetCorsHeaders()
        {
            var context = FrapidHttpContext.GetCurrent();

            if (context == null)
            {
                return;
            }

            bool isFont = this.IsFont(context.Request.PhysicalPath);

            if (!isFont)
            {
                return;
            }

            context.Response.Headers.Set("Access-Control-Allow-Origin", "*");
            context.Response.Headers.Set("Vary", "Origin");
            context.Response.Headers.Set("Access-Control-Allow-Headers", "Content-Type");
            context.Response.Headers.Set("Access-Control-Allow-Methods", "HEAD,GET");
            context.Response.Headers.Set("Access-Control-Allow-Credentials", "true");
        }
Example #16
0
        public async Task <IEnumerable <SearchResultContent> > SearchAsync(string tenant, string query)
        {
            var result = await Contents.SearchAsync(tenant, query).ConfigureAwait(false);

            var context = FrapidHttpContext.GetCurrent();

            if (context == null)
            {
                return(new List <SearchResultContent>());
            }

            string domain = TenantConvention.GetBaseDomain(new HttpContextWrapper(context), true);

            return(result.Select(item => new SearchResultContent
            {
                Title = item.Title,
                Contents = item.Contents.ToText().Truncate(200),
                LastUpdatedOn = item.LastEditedOn,
                LinkUrl = item.IsBlog
                    ? UrlHelper.CombineUrl(domain, "/blog/" + item.CategoryAlias + "/" + item.Alias)
                    : UrlHelper.CombineUrl(domain, "/site/" + item.CategoryAlias + "/" + item.Alias)
            }).ToList());
        }