Пример #1
0
        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="actionContext">当前异常</param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            base.OnActionExecuting(actionContext);

            var returnType             = "application/json";
            var fileType               = "multipart/form-data";
            HttpRequestMessage request = actionContext.Request;

            request.Headers.Accept.Clear();
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(returnType));
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(fileType));
            request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("zh-cn", 0.5));
            request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("en-us", 0.5));
            request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

            System.Collections.ObjectModel.Collection <CookieHeaderValue> cookies = actionContext.Request.Headers.GetCookies("SessionID");

            ApiBaseController bc = (ApiBaseController)actionContext.ControllerContext.Controller;

            //初始化Cookie和Session以及当前登录用户
            InitiSession(cookies, actionContext, bc);

            //检查当前Action是否需要身份验证和授权才能执行
            CheckLoginAndOperation(actionContext, bc);
        }
Пример #2
0
        /// <summary>
        /// 拼装异常信息与环境信息
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <param name="ex">异常</param>
        /// <returns>异常信息与环境信息文本</returns>
        private static string GetExceptionMessage(HttpActionExecutedContext context, Exception ex)
        {
            string            paramData = Evt.Framework.Common.JsonUtil.Serialize(context.ActionContext.ActionArguments);
            ApiBaseController bc        = (ApiBaseController)context.ActionContext.ControllerContext.Controller;
            string            sessionID = bc.Session.SessionID;

            return(ex.ToString() + "\r\n  ParamData:" + paramData + "  SessionID:" + sessionID + "\r\n  URL:" + context.Request.RequestUri + "\r\n\r\n");
        }
Пример #3
0
        /// <summary>
        /// 检查当前Action是否需要身份验证和授权才能执行
        /// </summary>
        /// <param name="actionContext">HttpActionContext</param>
        /// <param name="bc">BaseController</param>
        private void CheckLoginAndOperation(HttpActionContext actionContext, ApiBaseController bc)
        {
            var attributes = actionContext.ActionDescriptor.GetCustomAttributes <NonAuthorizeAttribute>();

            //验证是否需要登录
            if (attributes != null && attributes.Count > 0)
            {
                return;
            }

            //验证是否已经登录
            bool isLogin = true;

            if (bc.Session.ContainsKey(ConfigUtil.SystemUserSessionKey))
            {
                var currUser = bc.Session[ConfigUtil.SystemUserSessionKey] as LoginInfo;
                isLogin = currUser != null;
            }
            else
            {
                isLogin = false;
            }

            if (!isLogin && !actionContext.Request.RequestUri.LocalPath.ToLower().Equals("/shop/dologout"))
            {
                var noSessionMsg = "会话超时,请重新登录!";
                var current      = ServiceContext.Current;
                if (current != null && current.RequestTerminal != null && !string.IsNullOrWhiteSpace(current.RequestTerminal.ClientToken) && current.RequestTerminal.ClientToken != "null")
                {
                    bc.SingleLoginOn(current.RequestTerminal.ClientToken);
                    var currUser = SingleInstance <ShopBLL> .Instance.DoLogin(null, current.RequestTerminal.ClientToken);

                    if (currUser != null)
                    {
                        var loginInfo = new LoginInfo()
                        {
                            UserId = currUser.ShopId, Mobile = currUser.ShopAccount, AccessToken = currUser.AccessToken
                        };
                        bc.Session[ConfigUtil.SystemUserSessionKey] = loginInfo;
                    }
                    else
                    {
                        throw new Evt.Framework.Common.AuthenticationException(noSessionMsg);
                    }
                }
                else
                {
                    throw new Evt.Framework.Common.AuthenticationException(noSessionMsg);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="actionExecutedContext">异常类型</param>
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            ApiBaseController bc         = (ApiBaseController)actionExecutedContext.ActionContext.ControllerContext.Controller;
            string            actionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName ?? string.Empty;

            if (actionExecutedContext.Response != null)
            {
                actionExecutedContext.Response.Headers.AddCookies(new CookieHeaderValue[] { new CookieHeaderValue("SessionID", bc.Session.SessionID)
                                                                                            {
                                                                                                Path = "/"
                                                                                            } });

                //如果请求不带版本号,则添加响应头以指示浏览器不缓存当前请求结果
                if (actionExecutedContext.ActionContext.Request.Properties.ContainsKey("v"))
                {
                    actionExecutedContext.ActionContext.Response.Headers.Add("Pragma", "no-cache");
                    actionExecutedContext.ActionContext.Response.Headers.Add("Expires", "0");
                }
            }

            base.OnActionExecuted(actionExecutedContext);
        }
Пример #5
0
        /// <summary>
        /// 初始化Session和当前用户
        /// </summary>
        /// <param name="cookies">Collection</param>
        /// <param name="bc">BaseController</param>
        private void InitiSession(Collection <CookieHeaderValue> cookies, HttpActionContext actionContext, ApiBaseController bc)
        {
            if (cookies == null || cookies.Count == 0)
            {
                bc.Session = SessionManager.CreateSession();
            }
            else
            {
                string sessionID = string.Empty;
                foreach (CookieState cookieState in cookies[0].Cookies)
                {
                    if (cookieState.Name == "SessionID")
                    {
                        sessionID = cookieState.Value;
                        break;
                    }
                }
                Session session = SessionManager.GetSession(sessionID);

                if (session == null)
                {
                    bc.Session = SessionManager.CreateSession();
                }
                else
                {
                    bc.Session = session;
                }
            }

            //获取客户端信息
            var clientModel = new RequestClientInfoModel();

            if (bc.Session.ContainsKey(ConfigUtil.SystemTerminalSessionKey))
            {
                var clientSeesion = bc.Session[ConfigUtil.SystemTerminalSessionKey];
                if (clientSeesion != null)
                {
                    clientModel = bc.Session[ConfigUtil.SystemTerminalSessionKey] as RequestClientInfoModel;
                }
            }
            else
            {
                clientModel          = GetClientInfo(actionContext.Request);
                clientModel.ClientIP = NetUtil.Ip;
                bc.Session[ConfigUtil.SystemTerminalSessionKey] = clientModel;
            }

            //设置Service中的ContextUser
            ServiceContext.Current.ContextUser = new SysServericeContext(bc.Session.SessionID);

            //设置Service中的RequestTerminal
            ServiceContext.Current.RequestTerminal = new PrdRequestTerminal(bc.Session.SessionID);
        }