コード例 #1
0
        public async Task <UserSession> GetAsync(string id)
        {
            var value = await _distributedCache.GetStringAsync(GetSessionKey(id));

            var data = ProtectionUtils.DeserializeAndUnprotect <IDictionary <string, string> >(value, _dataProtector);

            return(new UserSession(id, data));
        }
コード例 #2
0
        public void CreateIdCookie(UserSession session)
        {
            var cookieOptions = CreateCookieOptions();

            var value = ProtectionUtils.SerializeAndProtect(session.Id, _dataProtector);

            HttpContext.Response.Cookies.Append(CookieName, value, cookieOptions);
        }
コード例 #3
0
        public string GetIdFromCookie()
        {
            if (!IsCookieExist)
            {
                return(null);
            }

            if (!HttpContext.Request.Cookies.TryGetValue(CookieName, out var value))
            {
                return(null);
            }

            return(string.IsNullOrWhiteSpace(value)
                ? null
                : ProtectionUtils.DeserializeAndUnprotect <string>(value, _dataProtector));
        }
コード例 #4
0
        public Task SetAsync(UserSession userSession)
        {
            if (userSession == null)
            {
                throw new ArgumentNullException(nameof(userSession));
            }

            var id = userSession.Id;

            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentException("Session id is empty.");
            }

            var session = ProtectionUtils.SerializeAndProtect(userSession.Data, _dataProtector);

            return(_distributedCache.SetStringAsync(GetSessionKey(id), session));
        }