コード例 #1
0
        /// <summary>
        /// 获取当前回话的用户ID,如果没有用户ID,则会创建一个用户ID
        /// </summary>
        /// <param name="context">请求上下文</param>
        /// <param name="noCache">是否跳过缓存</param>
        /// <returns></returns>
        public static string GetUserId(HttpContext context, bool noCache = false)
        {
            HttpCookie cookie          = context.Request.Cookies.Get("uid");
            string     userId          = string.Empty;
            string     cookieValue     = string.Empty;
            var        userApplication = new UserApplication();

            // 没有用户ID,并且验证cookie合法 => 用户id是否存在,否则直接创建一个
            if (cookie != null && cookie.Value != null && cookie.Value.Trim().Length <= 40 /*said 中 md5 生成的 uuid 只有32位*/)
            {
                cookieValue = cookie.Value.Trim();

                // 第二层检测,如果从 cache 里面没有检测到 uid
                if (CacheHelper.GetCache(cookieValue) != null)
                {
                    userId = cookieValue;
                }
                // 从数据库查询检测,并将数据库结果缓存
                if (userId == string.Empty && (noCache ?
                                               userApplication.ExistsNoCache(cookie.Value) : // 配置了跳过缓存
                                               userApplication.Exists(cookie.Value)))        // EF默认有缓存,检索用户是否存在*应该*更快
                {
                    // 确认是合法的 cookie
                    userId = cookieValue;
                    // 放入缓存
                    CacheHelper.SetCache(cookieValue, userId);
                }
            }

            try
            {
                // 还没有找到 uid,则重新生成一个
                if (string.Empty == userId)
                {
                    cookie = new HttpCookie("uid");
                    userId = SaidCommon.GUID;
                    User user = new User
                    {
                        UserID = userId,
                        EMail  = string.Empty,
                        Name   = string.Empty,
                        Date   = DateTime.Now,
                        //如果是管理员则种下管理员的GUID,否则重新生成一个普通用户的GUID
                        SecretKey = context.Session["adminId"] != null ? context.Session["adminId"] as string : SaidCommon.GUID,
                        //管理员则标记上管理员身份
                        Rule = context.Session["adminId"] != null ? 1 : 0,
                        IsSubscribeComments = true,
                        Site = string.Empty
                    };
                    userApplication.Add(user);
                    if (userApplication.Commit())
                    {
                        cookie.Name  = "uid";
                        cookie.Value = userId;
                        //cookie.Values.Add("id", userId);
                        cookie.Path    = "/";
                        cookie.Expires = DateTime.Now.AddYears(1);
                        context.Response.Cookies.Add(cookie);
                    }
                }
            }
            catch (Exception e)
            {
                logManager.Error("获取用户信息发生异常", e);
                throw e;
            }
            context.Session["userId"] = userId;
            return(userId);
        }