AppendCookie() public méthode

public AppendCookie ( Cookie cookie ) : void
cookie Cookie
Résultat void
		/// <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();
		}
Exemple #2
0
 private void SetCookies(HttpListenerResponse resp)
 {
     if ( _cookies == null ) { return; }
     foreach (Cookie cookie in _cookies)
     {
         resp.AppendCookie(cookie);
     }
 }
Exemple #3
0
        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;
        }