Esempio n. 1
0
 protected MvcHtmlString GetText(string key)
 {
     return(new MvcHtmlString(LanguageHelper.GetText(key)));
 }
Esempio n. 2
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (NeedAuth)
            {
                if (!ValidateAuth())
                {
                    //判断客户端的请求类型,Ajax或普通请求
                    if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                    {
                        string acceptType = filterContext.RequestContext.HttpContext.Request.Headers["Accept"];
                        string message    = LanguageHelper.GetText("未登录或身份验证信息已经过期,请重新登录!");

                        if (acceptType.Contains("application/json"))
                        {
                            var data = new
                            {
                                error   = true,
                                message = message,
                                code    = 401
                            };
                            filterContext.Result = new JsonResult {
                                Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                            };
                        }
                        else
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("<?xml version=\"1.0\"?>");
                            sb.AppendLine("<result>");
                            sb.AppendLine("<error>true</error>");
                            sb.AppendLine("<message>" + message + "</message>");
                            sb.AppendLine("</result>");
                            filterContext.Result = new ContentResult
                            {
                                Content         = sb.ToString(),
                                ContentEncoding = Encoding.UTF8,
                                ContentType     = "application/xml"
                            };
                        }
                    }
                    else
                    {
                        string returnUrl = HttpUtility.UrlEncode(filterContext.RequestContext.HttpContext.Request.Url.AbsoluteUri);
                        string loginUrl  = ConfigurationManager.AppSettings["LoginUrl"];
                        if (string.IsNullOrEmpty(loginUrl))
                        {
                            loginUrl = "/Login?ReturnUrl=" + returnUrl;
                        }
                        else
                        {
                            loginUrl = loginUrl + "?ReturnUrl=" + returnUrl;
                        }
                        filterContext.Result = new RedirectResult(loginUrl);
                    }
                }
                else
                {
                    //验证权限信息,网站暂不需要
                }
            }
        }