/// <summary>
        /// Save the session into the response
        /// </summary>
        /// <param name="session">Session to save</param>
        /// <param name="response">Response to save into</param>
        public void Save(ISession session, Nancy.Response response)
        {
            this.ExpireOldSessions();

            if (session == null || !session.HasChanged)
            {
                return;
            }

            var id = session["_id"] as string;

            if (null == id)
            {
                // TODO: warn
                return;
            }

            // Persist the session
            session.Delete("_id");
            Await(RethinkDbSessionStore.UpdateSession(currentConfiguration, id, session));

            // Encrypt the session Id in the cookie
            var cryptographyConfiguration = this.currentConfiguration.CryptographyConfiguration;
            var encryptedData             = cryptographyConfiguration.EncryptionProvider.Encrypt(id);
            var hmacBytes  = cryptographyConfiguration.HmacProvider.GenerateHmac(encryptedData);
            var cookieData = HttpUtility.UrlEncode(String.Format("{0}{1}", Convert.ToBase64String(hmacBytes), encryptedData));

            var cookie = new NancyCookie(this.currentConfiguration.CookieName, cookieData, true)
            {
                Domain = this.currentConfiguration.Domain,
                Path   = this.currentConfiguration.Path
            };

            response.WithCookie(cookie);
        }
Beispiel #2
0
        public AuthModule()
            : base("/api")
        {
            Post["/authenticate"] = x => {
                var bind = this.Bind<LoginRequest>();

                UserManager<User> manager = new UserManager<User>(new UserStore());

                var user = manager.Find(bind.Username, bind.Password);

                if (user == null)
                {
                    return new Response
                    {
                        StatusCode = HttpStatusCode.Unauthorized
                    };
                }
                else
                {
                    var response = new Response
                    {
                        StatusCode = HttpStatusCode.OK
                    };
                    return response.WithCookie("sq-valid", user.UserName, DateTime.Now.AddMinutes(5));
                }
            };

            Get["/logout"] = x => {
                var response = new Response
                {
                    StatusCode = HttpStatusCode.OK
                };
                return response.WithCookie("sq-valid", null, DateTime.Now.AddYears(-5));
            };
        }
Beispiel #3
0
        protected Nancy.Response InSession(Nancy.Response response = null)
        {
            if (response == null)
            {
                throw new Exception("null response object");
            }

            bool a = CurrentSession.Valid;             // Этот запуск свойства создает анонима в анонимной сессии.

            activeSessions[CurrentSession.GUID] = CurrentSession;

            return(response.WithCookie(IN_SESSION_COOKIE_NAME, CurrentSession.GUID, DateTime.Today.AddYears(1)));
            // Установить "исчезновение" куки на один год вперед.
        }
        protected Nancy.Response InSession(Nancy.Response response = null)
        {
            if (response == null)
            {
                throw new RenderException("null response object");
            }

            if (String.IsNullOrEmpty(CurrentSession.GUID))
            {
                CurrentSession.GUID     = ImportFromAtlcomru.GetGUID();
                CurrentSession["valid"] = "false";
            }

            activeSessions[CurrentSession.GUID] = CurrentSession;

            return(response.WithCookie(IN_SESSION_COOKIE_NAME, CurrentSession.GUID));
        }
        public void Save(ISession session, Response response)
        {
            if (session == null || !session.HasChanged)
            {
                return;
            }

            string sessionID = null;

            if (!response.Cookies.Any(cookie => cookie.Name == cookieName))
            {
                sessionID = Convert.ToBase64String(Encoding.UTF8.GetBytes(DateTime.UtcNow.ToLongTimeString()));
                response.WithCookie(new NancyCookie(cookieName, sessionID));
            }
            else
            {
                sessionID = response.Cookies.Where(cookie => cookie.Name == cookieName).Select(cookie => cookie.Value).First();
            }

            store[sessionID] = session.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
        }