Пример #1
0
        //protected virtual void SetUserCookie(Guid userGuid)
        //{
        //    if (_httpContext == null || _httpContext.Response == null) return;

        //    var cookie = new HttpCookie(UserCookieName)
        //    {
        //        HttpOnly = true,
        //        Value = userGuid.ToString(),
        //        //if user Guid is empty, expire the cookie immediately, else extend it as per configured duration
        //        Expires = userGuid == Guid.Empty ? DateTime.Now.AddMonths(-1) : DateTime.Now.AddHours(CookieExpires)
        //    };
        //    if (_httpContext.Response.Cookies[UserCookieName] != null)
        //    {
        //        _httpContext.Response.Cookies[UserCookieName].Value = userGuid.ToString();
        //        if (userGuid == Guid.Empty)
        //            _httpContext.Response.Cookies.Add(cookie);

        //    }
        //    else
        //    {
        //        _httpContext.Response.Cookies.Add(cookie);
        //    }


        //}
        /// <summary>
        /// Creates a new session for a user and also reads & creates the required cookies. Makes an API call, creates a new user session record with deviceId, UserId, SessionId
        /// Triggers: Session_Start, Logout
        /// In case, its triggered from Logout, the userId & sessionId are reset and a new Session is generated - however device Id is retained.
        /// </summary>
        /// <param name="resetSession">passed as true at the time of Logout</param>
        /// <returns></returns>
        public async Task <string> CreateUserSession(bool resetSession = false)
        {
            if (_httpContext == null || _httpContext.Response == null)
            {
                return("");
            }

            var session = new SessionInfo
            {
                IpAddress = Utils.GetCurrentIpAddress(),
                Browser   = Utils.GetBrowserInfo(),
                Referrer  = Utils.GetReferrer(),
                Utm       = Utils.GetUtm()
            };

            if (CurrentUser != null && CurrentUser.UserId != null)
            {
                session.CustomerId = CurrentUser.UserId.ToString();
            }

            if (_httpContext.Request.Cookies[Constants.COOKIE_DEVICEID] != null)
            {
                session.DeviceId = _httpContext.Request.Cookies[Constants.COOKIE_DEVICEID].Value;
            }
            else // if deviceId Cookie does not exist, create a new deviceID
            {
                session.DeviceId = Guid.NewGuid().ToString();
            }



            //if (_httpContext.Request.Cookies[Constants.COOKIE_USERID] != null)
            //    session.CustomerId = _httpContext.Request.Cookies[Constants.COOKIE_USERID].Value;
            if (_httpContext.Request.Cookies[Constants.COOKIE_SESSIONID] != null && resetSession == false)
            {
                session.SessionId = _httpContext.Request.Cookies[Constants.COOKIE_SESSIONID].Value;
            }
            if (string.IsNullOrEmpty(session.SessionId))
            {
                var cookie_basketId = new HttpCookie(Constants.COOKIE_BASKETID)
                {
                    HttpOnly = true, Value = "", Expires = DateTime.Now.AddDays(-1)
                };
                _httpContext.Response.Cookies.Add(cookie_basketId);

                var response = await _sessionRepository.CreateUserSessionAsync(session);

                session.SessionId = response.Result;
            }

            //if (resetSession)
            //{
            //    // session.SessionId = "";
            //    session.CustomerId = "";
            //}


            //var cookie_userId = new HttpCookie(Constants.COOKIE_USERID){HttpOnly = true,Value = session.CustomerId,Expires = DateTime.Now.AddDays(Constants.COOKIE_USERID_EXPIRES_DAYS)};
            var cookie_deviceId = new HttpCookie(Constants.COOKIE_DEVICEID)
            {
                HttpOnly = true, Value = session.DeviceId, Expires = DateTime.Now.AddDays(Constants.COOKIE_DEVICEID_EXPIRES_DAYS)
            };
            var cookie_sessionId = new HttpCookie(Constants.COOKIE_SESSIONID)
            {
                Value = session.SessionId, Expires = DateTime.Now.AddMinutes(Constants.COOKIE_SESSIONID_EXPIRES_MINUTES)
            };

            //_httpContext.Response.Cookies.Add(cookie_userId);
            _httpContext.Response.Cookies.Add(cookie_deviceId);
            _httpContext.Response.Cookies.Add(cookie_sessionId);
            return(session.SessionId);
        }
Пример #2
0
        //protected virtual void SetUserCookie(Guid userGuid)
        //{
        //    if (_httpContext == null || _httpContext.Response == null) return;

        //    var cookie = new HttpCookie(UserCookieName)
        //    {
        //        HttpOnly = true,
        //        Value = userGuid.ToString(),
        //        //if user Guid is empty, expire the cookie immediately, else extend it as per configured duration
        //        Expires = userGuid == Guid.Empty ? DateTime.Now.AddMonths(-1) : DateTime.Now.AddHours(CookieExpires)
        //    };
        //    if (_httpContext.Response.Cookies[UserCookieName] != null)
        //    {
        //        _httpContext.Response.Cookies[UserCookieName].Value = userGuid.ToString();
        //        if (userGuid == Guid.Empty)
        //            _httpContext.Response.Cookies.Add(cookie);

        //    }
        //    else
        //    {
        //        _httpContext.Response.Cookies.Add(cookie);
        //    }


        //}
        /// <summary>
        /// Creates a new session for a user and also reads & creates the required cookies. Makes an API call, creates a new user session record with deviceId, UserId, SessionId
        /// Triggers: Session_Start, Logout
        /// In case, its triggered from Logout, the userId & sessionId are reset and a new Session is generated - however device Id is retained.
        /// </summary>
        /// <param name="resetSession">passed as true at the time of Logout</param>
        /// <returns></returns>
        public async Task <string> CreateUserSession(bool resetSession = false)
        {
            if (_httpContext == null || _httpContext.Response == null)
            {
                return("");
            }

            var session = new SessionInfo
            {
                IpAddress = Utils.GetCurrentIpAddress(),
                Browser   = Utils.GetBrowserInfo(),
                Referrer  = Utils.GetReferrer(),
                Utm       = Utils.GetUtm()
            };
            var httpContext = System.Web.HttpContext.Current;


            if (CurrentUser != null && CurrentUser.UserId != null)
            {
                session.CustomerId = CurrentUser.UserId.ToString();
            }
            else
            {
                if (httpContext.Request.QueryString["email"] != null)
                {
                    var user = _customerRepository.GetExistingUser(httpContext.Request.QueryString["email"])?.Result?[0];

                    _httpContext.Session[Constants.SESSION_USERID]       = user.UserId;
                    _httpContext.Session[Constants.SESSION_COMPANYID]    = user.CompanyId;
                    _httpContext.Session[Constants.SESSION_ISGHOSTLOGIN] = user.IsGhostLogin;
                    _httpContext.Session[Constants.SESSION_ADMINUSER]    = user.AdminUserName;
                    if (!Enum.IsDefined(typeof(CompanyUserRole), user.CompanyUserRole)) //Added check for Enum null
                    {
                        _httpContext.Session[Constants.SESSION_COMPANYUSERROLE] = (CompanyUserRole)user.CompanyUserRole.GetHashCode();
                    }

                    //stored the user object in session.
                    _httpContext.Session[Constants.SESSION_CACHED_USER] = user;
                }
            }
            if (_httpContext.Request.Cookies[Constants.COOKIE_DEVICEID] != null)
            {
                session.DeviceId = _httpContext.Request.Cookies[Constants.COOKIE_DEVICEID].Value;
            }
            else // if deviceId Cookie does not exist, create a new deviceID
            {
                session.DeviceId = Guid.NewGuid().ToString();
            }



            //if (_httpContext.Request.Cookies[Constants.COOKIE_USERID] != null)
            //    session.CustomerId = _httpContext.Request.Cookies[Constants.COOKIE_USERID].Value;
            if (_httpContext.Request.Cookies[Constants.COOKIE_SESSIONID] != null && resetSession == false)
            {
                session.SessionId = _httpContext.Request.Cookies[Constants.COOKIE_SESSIONID].Value;
            }
            if (string.IsNullOrEmpty(session.SessionId))
            {
                var cookie_basketId = new HttpCookie(Constants.COOKIE_BASKETID)
                {
                    HttpOnly = true, Value = "", Expires = DateTime.Now.AddDays(-1)
                };
                _httpContext.Response.Cookies.Add(cookie_basketId);

                var response = await _sessionRepository.CreateUserSessionAsync(session);

                session.SessionId = response.Result;
            }

            //if (resetSession)
            //{
            //    // session.SessionId = "";
            //    session.CustomerId = "";
            //}


            //var cookie_userId = new HttpCookie(Constants.COOKIE_USERID){HttpOnly = true,Value = session.CustomerId,Expires = DateTime.Now.AddDays(Constants.COOKIE_USERID_EXPIRES_DAYS)};
            var cookie_deviceId = new HttpCookie(Constants.COOKIE_DEVICEID)
            {
                HttpOnly = true, Value = session.DeviceId, Expires = DateTime.Now.AddDays(Constants.COOKIE_DEVICEID_EXPIRES_DAYS)
            };
            var cookie_sessionId = new HttpCookie(Constants.COOKIE_SESSIONID)
            {
                Value = session.SessionId, Expires = DateTime.Now.AddMinutes(Constants.COOKIE_SESSIONID_EXPIRES_MINUTES)
            };

            //_httpContext.Response.Cookies.Add(cookie_userId);
            _httpContext.Response.Cookies.Add(cookie_deviceId);
            _httpContext.Response.Cookies.Add(cookie_sessionId);
            return(session.SessionId);
        }