コード例 #1
0
        private string BuildLoginUrl(string returnUrl)
        {
            //return "/login/?returnUrl=" + HttpUtility.UrlEncode(returnUrl);
            string routeName = ConfigurationManager.AppSettings["LoginRouteName"];

            if (string.IsNullOrWhiteSpace(routeName))
            {
                routeName = "Login";
            }
            return(RouteHelper.BuildUrl(routeName, new { returnUrl = HttpUtility.UrlEncode(returnUrl) }));
        }
コード例 #2
0
        private string BuildNoAuthorizedUrl(string returnUrl)
        {
            //return "/login/?returnUrl=" + HttpUtility.UrlEncode(returnUrl);
            string routeName = ConfigurationManager.AppSettings["NoAuthorizedRouteName"];

            if (string.IsNullOrWhiteSpace(routeName))
            {
                routeName = "NoAuthorized";
            }
            return(RouteHelper.BuildUrl(routeName));
        }
コード例 #3
0
        protected override bool CheckAuthentication()
        {
            var  routeItem = RouteHelper.GetCurrentConfigRouteItem();
            bool need      = routeItem == null ? false : routeItem.NeedLogin;

            if (need == false)
            {
                return(true);
            }
            return(base.CheckAuthentication());
        }
コード例 #4
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string userAgent = filterContext.HttpContext.Request.UserAgent;

            if (string.IsNullOrWhiteSpace(userAgent) || // http头里的user agent为空的
                filterContext.HttpContext.Request.BySearchEngine() || // 来自搜索引擎的爬虫的请求
                filterContext.HttpContext.Request.ByApp() || // 来自App客户端的请求
                filterContext.HttpContext.Request.IsAjaxRequest() ||  // 来自于Ajax的请求
                string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.InvariantCultureIgnoreCase) == false    // 来自非Get方式的请求(如Post)
                )
            {
                // 不做任何基于设备类型的页面跳转
                return;
            }
            DeviceType?dType = RouteHelper.CurrentSupportedDeviceTypeInConfig;

            if (dType == null) // 没有特别设置当前页面所支持的设备
            {
                return;
            }
            DeviceType curType = DeviceNameDetector.GetDeviceType(userAgent);

            if (curType == dType.Value) // 当前访问设备就是特别设置的支持的设备
            {
                return;
            }
            string url = RouteHelper.RedirectUrlForUnsupportedDevices;

            if (string.IsNullOrWhiteSpace(url)) // 没有设定Redirect到的地址
            {
                return;
            }
            if (url.IndexOf('{') >= 0 && url.IndexOf('}') > 0)
            {
                var rdata      = RouteHelper.GetCurrentRouteData();
                var collection = rdata.Values;
                if (collection != null && collection.Count > 0)
                {
                    // 可能配置的跳转目标是一个url模板,那么需要使用当前请求的模板参数的值来应用到跳转到的模板url里
                    foreach (var entry in collection)
                    {
                        url = url.Replace("{" + entry.Key + "}", entry.Value == null ? string.Empty : entry.Value.ToString());
                    }
                }
            }
            if (filterContext.HttpContext.Request.QueryString.Count > 0) // 当前请求带有QueryString参数,那么需要将querystring参数排序后拼接到跳转目标的url上去
            {
                url = url + filterContext.HttpContext.Request.GetSortedQueryString(true);
            }
            filterContext.Result = new RedirectResult(url, false);
        }
コード例 #5
0
            public override void Write(byte[] buffer, int offset, int count)
            {
                if (m_Response.StatusCode != 200) // 比如301或302跳转的
                {
                    m_Filter.Write(buffer, offset, count);
                    return;
                }
                string clientToken = m_Request.Headers["If-None-Match"];

                if (m_ETag != clientToken)
                {
                    var item = RouteHelper.GetCurrentConfigRouteItem();
                    if (item.ServerCacheExpiredMinutes > 0 && m_HasCacheItem == false) // 需要服务器端缓存,但还没有缓存
                    {
                        var citem = m_HttpContext.Items["RouteCacheFilterAttribute.citem"] as CacheItem;
                        if (citem == null) // 这样做目的是为了确保Write方法可能被多次调用时不会出错
                        {
                            citem = new CacheItem {
                                Html = new List <byte>(count)
                            };
                            for (int i = offset; i < count; i++)
                            {
                                citem.Html.Add(buffer[i]);
                            }
                            string key = GenerateServerCacheKey(m_HttpContext);
                            m_HttpContext.Items["RouteCacheFilterAttribute.citem"] = citem;
                            Cache.SetLocalCache(key, citem, true, item.ServerCacheExpiredMinutes); // 此时将缓存数据放入全局Cache中,可以被其他访问请求看到和读取了
                        }
                        else
                        {
                            for (int i = offset; i < count; i++)
                            {
                                citem.Html.Add(buffer[i]);
                            }
                        }
                    }
                    m_Response.AddHeader("ETag", m_ETag);
                    m_Filter.Write(buffer, offset, count);
                }
                else
                {
                    m_Response.SuppressContent   = true;
                    m_Response.StatusCode        = 304;
                    m_Response.StatusDescription = "Not Modified";
                    //m_Response.AddHeader("Content-Length", "0");
                }
            }
コード例 #6
0
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var       item  = RouteHelper.GetCurrentConfigRouteItem();
            CacheItem citem = null;

            if (item.ServerCacheExpiredMinutes > 0) // 需要服务端缓存
            {
                citem = filterContext.HttpContext.Items["RouteCacheFilterAttribute.citem"] as CacheItem;
            }
            if (item == null || item.ClientCache == ClientCacheMode.Default)
            {
                filterContext.HttpContext.Response.Filter = new ServerCacheFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
                return;
            }
            if (item.ClientCache == ClientCacheMode.None)
            {
                // 让每一次的etag都不一样,防止iis级别上的Etag缓存设置
                filterContext.HttpContext.Response.AddHeader("ETag", Guid.NewGuid().ToString());
                // abandon cache
                filterContext.HttpContext.Response.Expires         = -1;
                filterContext.HttpContext.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
                filterContext.HttpContext.Response.CacheControl    = "no-cache";
                filterContext.HttpContext.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
                filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                filterContext.HttpContext.Response.Cache.SetNoStore();
                filterContext.HttpContext.Response.Filter = new ServerCacheFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
            }
            else if (item.ClientCache == ClientCacheMode.Permanent304)
            {
                string etag = item.ETag;
                if (string.IsNullOrWhiteSpace(etag))
                {
                    etag = s_ETag;
                }
                filterContext.HttpContext.Response.Filter = new PermanentNotModifiedFilter(filterContext.HttpContext,
                                                                                           etag, item.ServerCacheExpiredMinutes, citem != null);
            }
            else if (item.ClientCache == ClientCacheMode.Unchanged304)
            {
                filterContext.HttpContext.Response.Filter = new ContentNotModifiedFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
            }
            else
            {
                filterContext.HttpContext.Response.Filter = new ServerCacheFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
            }
        }
コード例 #7
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var item = RouteHelper.GetCurrentConfigRouteItem();

            if (item.ServerCacheExpiredMinutes > 0)
            {
                string    key   = GenerateServerCacheKey(filterContext.HttpContext);
                CacheItem citem = Cache.GetLocalCache(key) as CacheItem;
                if (citem != null)
                {
                    string c = "1";
                    using (var stream = new MemoryStream(citem.Html.ToArray()))
                    {
                        using (var sr = new StreamReader(stream))
                        {
                            c = sr.ReadToEnd();
                        }
                    }
                    filterContext.HttpContext.Items["RouteCacheFilterAttribute.citem"]    = citem;
                    filterContext.HttpContext.Items["RouteCacheFilterAttribute.FindItem"] = "1";
                    filterContext.Result = new ContentResult()
                    {
                        Content = c
                    };                                                          // 避免再执行Action和View的Render
                    return;
                }
            }
            if (item.ClientCache == ClientCacheMode.Permanent304)
            {
                string etag = item.ETag;
                if (string.IsNullOrWhiteSpace(etag))
                {
                    etag = s_ETag;
                }
                string clientToken = filterContext.HttpContext.Request.Headers["If-None-Match"];
                if (etag == clientToken)
                {
                    filterContext.Result = new ContentResult()
                    {
                        Content = "1"
                    };                                                          // 避免再执行Action和View的Render
                    return;
                }
            }
            base.OnActionExecuting(filterContext);
        }
コード例 #8
0
        private string BuildLoginUrl(string returnUrl, HttpRequestBase request)
        {
            //return "/login/?returnUrl=" + HttpUtility.UrlEncode(returnUrl);
            string routeName = ConfigurationManager.AppSettings["LoginRouteName"];

            if (string.IsNullOrWhiteSpace(routeName))
            {
                routeName = "Login";
            }
            if (string.IsNullOrWhiteSpace(returnUrl))
            {
                return(RouteHelper.BuildUrl(routeName));
            }
            string x   = request.ByWeiXinBrowser() ? HttpUtility.UrlEncode(returnUrl) : returnUrl; // 因为微信会自动解码一次,所以需要编码两次
            var    url = RouteHelper.BuildUrl(routeName, new { returnUrl = x });

            return(url);
        }
コード例 #9
0
        protected string GetConfiguredSchema()
        {
            var    config = RouteHelper.GetCurrentConfigRouteItem();
            string domain = config == null ? string.Empty : config.Domain;
            object tmp;

            if (string.IsNullOrWhiteSpace(domain) &&
                RouteHelper.TryGetDataTokenOfCurrentRoute("_ibb_domain", out tmp) && tmp != null)
            {
                domain = tmp.ToString();
            }
            if (string.IsNullOrWhiteSpace(domain))
            {
                return("http");
            }
            if (domain.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
            {
                return("https");
            }
            return("http");
        }
コード例 #10
0
        protected override bool CheckNeedTrace(HttpContextBase httpContext)
        {
            var item = RouteHelper.GetCurrentConfigRouteItem();

            return(base.CheckNeedTrace(httpContext) && (item == null || item.Trace));
        }