当前位置: 首页>>代码示例>>C#>>正文


C# Response.WithCookie方法代码示例

本文整理汇总了C#中Response.WithCookie方法的典型用法代码示例。如果您正苦于以下问题:C# Response.WithCookie方法的具体用法?C# Response.WithCookie怎么用?C# Response.WithCookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Response的用法示例。


在下文中一共展示了Response.WithCookie方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CookieModule

        public CookieModule()
        {
            Get["/setcookie"] = _ =>
            {
                const string value = "HakLqr1OEdi+kQ/s92Rzz9hV1w/vzGZKqWeMQRHRJlwhbbgP87UELJZlYDfbVVLo";

                var cookie = new NancyCookie("testcookie", value);

                var response = new Response();
                response.WithCookie(cookie);
                response.StatusCode = HttpStatusCode.OK;

                return response;
            };

            Get["/getcookie"] = _ =>
            {
                const string value = "HakLqr1OEdi+kQ/s92Rzz9hV1w/vzGZKqWeMQRHRJlwhbbgP87UELJZlYDfbVVLo";

                var cookie = Context.Request.Cookies["testcookie"];

                return String.Equals(cookie, value) ?
                    HttpStatusCode.OK :
                    HttpStatusCode.InternalServerError;
            };
        }
开发者ID:jeremymeng,项目名称:Nancy,代码行数:26,代码来源:CookieModule.cs

示例2: Should_set_a_cookie_with_name_and_value

        public void Should_set_a_cookie_with_name_and_value()
        {
            // Given
            var response = new Response();

            // When
            response.WithCookie("itsover", "9000");

            // Then
            response.Cookies.Count.ShouldEqual(1);
            ValidateCookie(response.Cookies[0], "itsover", "9000", null, null, null);
        }
开发者ID:julfeng,项目名称:Nancy,代码行数:12,代码来源:ResponseFixture.cs

示例3: Should_set_a_cookie_with_everything

        public void Should_set_a_cookie_with_everything()
        {
            // Given
            var response = new Response();
            var date = DateTime.Now;

            // When
            response.WithCookie("itsover", "9000", date, "life", "/andeverything");

            // Then
            response.Cookies.Count.ShouldEqual(1);
            ValidateCookie(response.Cookies[0], "itsover", "9000", date, "life", "/andeverything");
        }
开发者ID:julfeng,项目名称:Nancy,代码行数:13,代码来源:ResponseFixture.cs

示例4: Should_output_the_responses_cookies

        public async Task Should_output_the_responses_cookies()
        {
            // Given
            var cookie1 = A.Fake<INancyCookie>();
            var cookie2 = A.Fake<INancyCookie>();
            var r = new Response();
            r.WithCookie(cookie1).WithCookie(cookie2);
            var nancyContext = new NancyContext { Response = r };

            A.CallTo(() => cookie1.ToString()).Returns("the first cookie");
            A.CallTo(() => cookie2.ToString()).Returns("the second cookie");

            SetupRequestProcess(nancyContext);

            // When
            await this.handler.ProcessRequest(context);

            // Then
            A.CallTo(() => this.response.AddHeader("Set-Cookie", "the first cookie")).MustHaveHappened();
            A.CallTo(() => this.response.AddHeader("Set-Cookie", "the second cookie")).MustHaveHappened();
        }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:21,代码来源:NancyHandlerFixture.cs

示例5: Save

        /// <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, Response response)
        {
            if (session == null || !session.HasChanged)
            {
                return;
            }

            var sb = new StringBuilder();
            foreach (var kvp in session)
            {
                sb.Append(HttpUtility.UrlEncode(kvp.Key));
                sb.Append("=");

                var objectString = this.currentConfiguration.Serializer.Serialize(kvp.Value);

                sb.Append(HttpUtility.UrlEncode(objectString));
                sb.Append(";");
            }

            var cryptographyConfiguration = this.currentConfiguration.CryptographyConfiguration;
            var encryptedData = cryptographyConfiguration.EncryptionProvider.Encrypt(sb.ToString());
            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);
        }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:36,代码来源:CookieBasedSessions.cs

示例6: Save

        /// <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, Response response)
        {
            Guid sessionId;

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

            if (session["__SessionId"] is Guid)
            {
                sessionId = (Guid)session["__SessionId"];
            }
            else
            {
                sessionId = Guid.NewGuid();
                session["__SessionId"] = sessionId;
                if (_currentConfiguration.SessionDuration != 0)
                    session["__SessionExpiration"] = DateTime.Now.AddSeconds(_currentConfiguration.SessionDuration);
            }

            var sb = new StringBuilder();
            foreach (var kvp in session)
            {
                sb.Append(HttpUtility.UrlEncode(kvp.Key));
                sb.Append("=");

                var objectString = _currentConfiguration.Serializer.Serialize(kvp.Value);

                sb.Append(HttpUtility.UrlEncode(objectString));
                sb.Append(";");
            }

            var cryptographyConfiguration = _currentConfiguration.CryptographyConfiguration;

            var redisData = cryptographyConfiguration.EncryptionProvider.Encrypt(sb.ToString());

            // Store the value in Redis
            _db.StringSet(_currentConfiguration.Prefix + sessionId.ToString(), redisData);

            if (_currentConfiguration.SessionDuration != 0)
                _db.KeyExpire(_currentConfiguration.Prefix + sessionId.ToString(), TimeSpan.FromSeconds(_currentConfiguration.SessionDuration));

            var encryptedSessionId = cryptographyConfiguration.EncryptionProvider.Encrypt(sessionId.ToString());
            var hmacBytes = cryptographyConfiguration.HmacProvider.GenerateHmac(sessionId.ToString());
            var cookieData = String.Format("{0}{1}", Convert.ToBase64String(hmacBytes), encryptedSessionId);

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

            if (_currentConfiguration.EnableSlidingSessions)
                session["__SessionExpiration"] = DateTime.Now.AddSeconds(_currentConfiguration.SessionDuration);

            if (session["__SessionExpiration"] is DateTime)
                cookie.Expires = (DateTime)session["__SessionExpiration"];

            response.WithCookie(cookie);
        }
开发者ID:richardprice,项目名称:Nancy.RedisSessionProvider,代码行数:66,代码来源:RedisBasedSessions.cs


注:本文中的Response.WithCookie方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。