/// <summary> /// Tries to log a user in. /// </summary> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <param name="search">The search part of the location where to redirect to on success.</param> /// <returns></returns> public Response LoginRequest(string username, string password, string search) { int userID = Proxy.SessionManager.UserID(username); if (userID == -1) { return PageWithErrorMessage("login.html", "wrong_username", "Username does not exist.", username); } else if (!Proxy.SessionManager.IsCorrectPW(username, password)) { return PageWithErrorMessage("login.html", "wrong_password", "Password is incorrect.", username); } // User+PW correct Logger.Metric(userID, "Login."); Proxy.SessionManager.LogUserIn(ClientIP, userID); // Set cookies. Cookie idCookie = new Cookie("uid", "" + userID, "/"); Cookie nameCookie = new Cookie("uname", username, "/"); DateTime expiryDate = Proxy.SessionManager.GetSessionExpiryDate(userID); idCookie.Expires = expiryDate; nameCookie.Expires = expiryDate; // We must use Headers.Add (Not AddHeader, not SetCookie and not AppendCookie) // as in the other cases only a comma separated list in one Set-Cookie would be created, // which browsers do not understand. C# sucks. _clientHttpContext.Response.Headers.Add("Set-Cookie", idCookie.ToCookieString()); _clientHttpContext.Response.Headers.Add("Set-Cookie", nameCookie.ToCookieString()); // Redirect to the User Homepage. _clientHttpContext.Response.Redirect("/trotro-user.html" + search); return new Response(); }
/// <summary> /// Sends expired cookies, so they will be deleted at the client side. /// </summary> private void SendLogoutCookies() { // Set cookies. Cookie idCookie = new Cookie("uid", "", "/"); Cookie nameCookie = new Cookie("uname", "", "/"); Cookie statusCookie = new Cookie("status", "", "/"); // Expiry date in the past. DateTime expiryDate = DateTime.Now.AddDays(-1); idCookie.Expires = expiryDate; nameCookie.Expires = expiryDate; statusCookie.Expires = expiryDate; _clientHttpContext.Response.Headers.Add("Set-Cookie", idCookie.ToCookieString()); _clientHttpContext.Response.Headers.Add("Set-Cookie", nameCookie.ToCookieString()); _clientHttpContext.Response.Headers.Add("Set-Cookie", statusCookie.ToCookieString()); }