I have the following code:
public class CookieCheckMiddleware
{
private readonly RequestDelegate _next;
public CookieCheckMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
if(httpContext.Request.Cookies["MyCookie"] == null && httpContext.Request.Path != "/WhereIShouldGo")
{
httpContext.Response.Redirect("/WhereIShouldGo");
}
await _next(httpContext); // calling next middleware
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class CookieCheckMiddlewareExtensions
{
public static IApplicationBuilder UseCookieCheckMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CookieCheckMiddleware>();
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseCookieCheckMiddleware();
...
}
It basically redirects to the captive portal if no cookie is set. Now I need to up a level - I need to somehow save the httpContext.Request.Path and forward to it RIGHT AFTER the user accepted the cookies. So setting a cookie beforehand is not an option, since the user hasn't accepted it yet... How could I accomplish that?
The solution was giving the request URL via the redirect like this:
httpContext.Response.Redirect("/Cookies?q="+ httpContext.Request.Path);and then via JavaScript getting the GET parameter and redirecting after clicking on the button.Case closed :-)