From 4407801a363aad16acf81f832b72fa65d26f4582 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 29 Jan 2021 22:50:19 +0100 Subject: [PATCH] Fix cookies --- Pages/Conference.cshtml | 4 ++-- Pages/Index.cshtml | 4 ++-- Pages/Info.cshtml | 4 ++-- c3stream.cs | 10 +++++++++- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Pages/Conference.cshtml b/Pages/Conference.cshtml index fcddad7..6a00265 100644 --- a/Pages/Conference.cshtml +++ b/Pages/Conference.cshtml @@ -9,7 +9,7 @@ return; } - c3stream.UpdateCookie(Request, Response, $"/Conference?c={Request.Query["c"]}"); + var cookie = c3stream.UpdateCookie(Request, Response, $"/Conference?c={Request.Query["c"]}"); ViewData["Title"] = Request.Query["c"]; var wc = new WebClient(); var conference = c3stream.Conferences.First(c => c.Acronym == Request.Query["c"]); @@ -36,7 +36,7 @@ @foreach (var talk in Request.Query["orderby"] == "published" ? conference.Talks.OrderByDescending(p => p.ReleaseDate) : conference.Talks.OrderBy(p => p.Date)) { - var state = states.FirstOrDefault(p => p.TalkId == talk.Guid && p.UserId == Request.Cookies["bookmark"])?.State; + var state = states.FirstOrDefault(p => p.TalkId == talk.Guid && p.UserId == cookie)?.State; var isWatched = state == "watched"; var isMarked = state == "marked"; var file = $"{talk.Slug}.mp4"; diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml index 859037a..ed6f0fb 100644 --- a/Pages/Index.cshtml +++ b/Pages/Index.cshtml @@ -2,13 +2,13 @@ @model IndexModel @{ ViewData["Title"] = "Home"; - c3stream.UpdateCookie(Request, Response, "/"); + var cookie = c3stream.UpdateCookie(Request, Response, "/"); }

Welcome to c3stream!

Your bookmark link:
- https://@Request.Host.Value?bookmark=@Request.Cookies["bookmark"]

+ https://@Request.Host.Value?bookmark=@cookie

@foreach (var conf in c3stream.Conferences) { @conf.Acronym diff --git a/Pages/Info.cshtml b/Pages/Info.cshtml index 171cc9a..0a429f6 100644 --- a/Pages/Info.cshtml +++ b/Pages/Info.cshtml @@ -10,12 +10,12 @@ return; } - c3stream.UpdateCookie(Request, Response, $"/Info?guid={Request.Query["guid"]}"); + var cookie = c3stream.UpdateCookie(Request, Response, $"/Info?guid={Request.Query["guid"]}"); await using var db = new Database.DbConn(); var talk = c3stream.GetEventByGuid(Request.Query["guid"]); - var state = db.States.FirstOrDefault(p => p.TalkId == Request.Query["guid"].ToString() && p.UserId == Request.Cookies["bookmark"])?.State; + var state = db.States.FirstOrDefault(p => p.TalkId == Request.Query["guid"].ToString() && p.UserId == cookie)?.State; if (talk == null) { Response.Redirect("/"); return; diff --git a/c3stream.cs b/c3stream.cs index 82706a4..70b2622 100644 --- a/c3stream.cs +++ b/c3stream.cs @@ -86,20 +86,28 @@ namespace c3stream { } } - public static void UpdateCookie(HttpRequest request, HttpResponse response, string redirectUri) { + public static string UpdateCookie(HttpRequest request, HttpResponse response, string redirectUri) { + var cookie = ""; //if new bookmark is in uri if (request.Query.ContainsKey("bookmark") && Guid.TryParseExact(request.Query["bookmark"], "D", out _)) { response.Cookies.Append("bookmark", request.Query["bookmark"], new CookieOptions {Expires = DateTimeOffset.MaxValue}); + cookie = request.Query["bookmark"]; } //if no cookie exists or cookie is invalid else if (!request.Cookies.ContainsKey("bookmark") || !Guid.TryParseExact(request.Cookies["bookmark"], "D", out _)) { var guid = Guid.NewGuid().ToString(); response.Cookies.Append("bookmark", guid, new CookieOptions {Expires = DateTimeOffset.MaxValue}); + cookie = guid; + } + else { + cookie = request.Cookies["bookmark"]; } if (request.Query.ContainsKey("bookmark")) { response.Redirect(redirectUri); } + + return cookie; } public static Event GetEventByGuid(string guid) {