本文整理汇总了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;
};
}
示例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);
}
示例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");
}
示例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();
}
示例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);
}
示例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);
}