コード例 #1
0
ファイル: WebCommon.cs プロジェクト: luobin991/GameTaurus
        /// <summary>
        /// 检查时间合法的字符串参数
        /// </summary>
        /// <returns></returns>
        public static string TimeString(string posts)
        {
            DateTime dt = DateTime.Now;

            //这里改为数组,原来用indexof会引起相似名称的参数冲突 kevin
            string[] tmp    = null;
            string   tmpstr = posts;

            if (!posts.StartsWith("http://") && !posts.StartsWith("?") && !posts.StartsWith("&"))
            {
                tmpstr = "&" + posts;
            }
            tmp = tmpstr.Split(new char[] { '?', '&' });
            List <string> tmpkey = new List <string>();

            for (int i = 0; i < tmp.Length; i++)
            {
                if (tmp[i].IndexOf("=") > 0)
                {
                    tmpkey.Add(tmp[i].Split('=')[0]);
                }
            }
            if (!tmpkey.Contains <string>("strdate"))
            {
                posts += "&strdate=" + HttpUtility.UrlEncode(dt.ToString());
            }
            if (!tmpkey.Contains <string>("strcode"))
            {
                posts += "&strcode=" + StringHelper.GetMd5("123" + dt.ToString() + "321");
            }
            //加上当前IP
            posts += "&sourceip=" + WebCommon.GetIPAddress();
            if (posts.StartsWith("http://") && posts.IndexOf("&") > 0 && posts.IndexOf("?") < 0)
            {
                posts = posts.Substring(0, posts.IndexOf("&")) + "?" + posts.Substring(posts.IndexOf("&") + 1);;
            }
            return(posts);
        }
コード例 #2
0
        /// <summary>
        /// 登录者信息添加到缓存中
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="appId">应用id</param>
        /// <param name="cookie">是否保存cookie,默认是</param>
        /// <returns></returns>
        public string AddLoginUser(string account, string appId, bool cookie = true)
        {
            string token = Guid.NewGuid().ToString();

            try
            {
                Operator operatorInfo = new Operator();
                operatorInfo.appId     = appId;
                operatorInfo.account   = account;
                operatorInfo.logTime   = DateTime.Now;
                operatorInfo.iPAddress = WebCommon.GetIPAddress();
                operatorInfo.browser   = WebCommon.GetClientBrowserVersions();
                operatorInfo.token     = token;

                if (cookie) //是否保存cookie
                {
                    string cookieMark = WebHelper.GetCookie(ReadonlyKey.LoginUserMarkKey).ToString();
                    if (string.IsNullOrWhiteSpace(cookieMark))
                    {
                        WebHelper.WriteCookie(ReadonlyKey.LoginUserMarkKey, operatorInfo.account);                  //写入登录者标识
                    }
                    WebHelper.WriteCookie(ReadonlyKey.cacheKeyOperator + operatorInfo.account, operatorInfo.token); //adms_operator_XX = token
                }

                WebHelper.WriteCookie(ReadonlyKey.LoginUserToken, operatorInfo.token);                         // 写入登录秘钥

                CacheHelper.Set <Operator>(ReadonlyKey.cacheKeyOperator + operatorInfo.account, operatorInfo); //写入缓存信息:当前连接用户

                return(operatorInfo.token);

                #region  code fail
                //if (cookie) //是否保存cookie
                //{
                //    string cookieMark = WebHelper.GetCookie(ReadonlyKey.LoginUserMarkKey).ToString(); //标记登录的浏览器
                //    if (string.IsNullOrWhiteSpace(cookieMark))
                //    {
                //        //operatorInfo.loginMark = Guid.NewGuid().ToString(); //创建登录者标识
                //        WebHelper.WriteCookie(ReadonlyKey.LoginUserMarkKey, operatorInfo.browser + "|" + operatorInfo.iPAddress); //写入登录者标识
                //    }
                //    else
                //        operatorInfo.loginMark = cookieMark;// 登录者标识 = 标记登录的浏览器

                //    WebHelper.WriteCookie(ReadonlyKey.LoginUserToken, token); // 写入登录秘钥
                //}
                //else
                //    operatorInfo.loginMark = loginMark; // 登录者标识 = 设备标识

                //当前用户信息
                //Dictionary<string, string> dic = new Dictionary<string, string>();
                //dic[ReadonlyKey.cacheKeyToken + account] = "1";
                //Dictionary<string, string> dicOld = CacheHelper.Get<Dictionary<string, string>>(ReadonlyKey.cacheKeyToken + account);
                //if (dicOld != null && dicOld.Count > 0)
                //{
                //    CacheHelper.Remove(ReadonlyKey.cacheKeyToken + account);
                //}
                //CacheHelper.Set<Dictionary<string, string>>(ReadonlyKey.cacheKeyToken + account, dic);

                //CacheHelper.Set<Operator>(ReadonlyKey.cacheKeyOperator + operatorInfo.loginMark, operatorInfo); //写入缓存信息:当前连接用户
                //return token;
                #endregion
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }