本文整理汇总了C#中System.Net.HttpListenerResponse.AppendCookie方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerResponse.AppendCookie方法的具体用法?C# HttpListenerResponse.AppendCookie怎么用?C# HttpListenerResponse.AppendCookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpListenerResponse
的用法示例。
在下文中一共展示了HttpListenerResponse.AppendCookie方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Send
/// <summary>
/// Automatically sends the appropriate response to the user agent.
/// </summary>
/// <param name="response">The response to set to this message.</param>
public virtual void Send(HttpListenerResponse response) {
Requires.NotNull(response, "response");
response.StatusCode = (int)this.Status;
MessagingUtilities.ApplyHeadersToResponse(this.Headers, response);
foreach (HttpCookie httpCookie in this.Cookies) {
var cookie = new Cookie(httpCookie.Name, httpCookie.Value) {
Expires = httpCookie.Expires,
Path = httpCookie.Path,
HttpOnly = httpCookie.HttpOnly,
Secure = httpCookie.Secure,
Domain = httpCookie.Domain,
};
response.AppendCookie(cookie);
}
if (this.ResponseStream != null) {
response.ContentLength64 = this.ResponseStream.Length;
this.ResponseStream.CopyTo(response.OutputStream);
}
response.OutputStream.Close();
}
示例2: SetCookies
private void SetCookies(HttpListenerResponse resp)
{
if ( _cookies == null ) { return; }
foreach (Cookie cookie in _cookies)
{
resp.AppendCookie(cookie);
}
}
示例3: IsAuthenticating
private bool IsAuthenticating(HttpListenerRequest aRequest, HttpListenerResponse aResponse)
{
string pathAndQuery = aRequest.Url.PathAndQuery;
string location;
if (String.Compare(aRequest.HttpMethod, "POST", true) == 0 && pathAndQuery == "/loginService")
{
MemoryStream memStream = new MemoryStream();
aRequest.InputStream.CopyTo(memStream);
byte[] bytes = memStream.ToArray();
XElement tree = XElement.Parse(Encoding.UTF8.GetString(bytes));
string username = tree.Element("username").Value;
string password = tree.Element("password").Value;
if (!iLoginValidator.ValidateCredentials(username, password))
{
aResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
aResponse.Close();
return true;
}
string guid = Guid.NewGuid().ToString();
lock (this)
{
iAuthenticatedClients.Add(guid, guid);
// TODO: write clients to xml file (iff not using session cookies)
}
aResponse.AppendCookie(new Cookie(kAuthCookieName, guid));
aResponse.StatusCode = (int)HttpStatusCode.OK;
location = "/";
byte[] buf = Encoding.UTF8.GetBytes(location + "\r\n");
aResponse.OutputStream.Write(buf, 0, buf.Length);
Logger.InfoFormat("Authenticated! Redirecting: {0} to {1}", pathAndQuery, location);
// just completed authentication. Redirect client to (assumed) original url
aResponse.Close();
return true;
}
foreach (Cookie cookie in aRequest.Cookies)
{
if (cookie.Name == kAuthCookieName && iAuthenticatedClients.ContainsKey(cookie.Value))
{
// already authenticated.
// A path of /{iForwardUdn} is a special case (see docs on our use of HaProxy) which needs to be redirected to "/"
if (pathAndQuery == String.Format("/{0}", iForwardUdn))
{
aResponse.Redirect("/");
aResponse.Close();
return true;
}
return false;
}
}
if (pathAndQuery == kLoginPath || pathAndQuery.StartsWith("/login/"))
// allow these requests through, regardless of our authentication state as they're needed to load the login screen
return false;
// redirect any other requests to the login page
location = kLoginPath;
aResponse.Redirect(location);
aResponse.Close();
Logger.InfoFormat("Redirecting: {0} to {1}", pathAndQuery, location);
return true;
}