在 index.html 下载的时刻设置 cookie

Set cookie at the monent when index.html downloading

我想在客户端下载时设置一个 http cookie index.html。

context.Response.Cookies.Append(key, value, new CookieOptions
            {
                Expires = new DateTimeOffset(DateTime.Now.AddDays(1)),
                HttpOnly = true,
                Secure = true,
                SameSite = SameSiteMode.Strict
            }); 

我不知道在哪里可以把它放到服务器代码中。

关于服务器上的index.html:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute();
            endpoints.MapFallbackToClientSideBlazor<Client.Program>("index.html");
        });

app.UseBlazorFrameworkFiles之前添加一个中间件,查看请求路径:

app.Use((context, next) =>
{
    if (PathIsApplicationPath(context.Request.Path))
    {
        SetApplicationCookie(context.Response);
    }
    return next();
});
app.UseBlazorFrameworkFiles();

...

private bool PathIsApplicationPath(PathString path)
{
     // TODO: implement this
}

private void SetApplicationCookie(HttpResponse response)
{
    response.Cookies.Append("TheCookieName", "TheCookieValue", new CookieOptions
            {
                Expires = new DateTimeOffset(DateTime.Now.AddDays(1)),
                HttpOnly = true,
                Secure = true,
                SameSite = SameSiteMode.Strict
            });
}