C
Join ServerC#
help
OnAppendCookie callback doesn't work
Nnobody.cs11/3/2022
I have an ASP.NET Core project, which uses IdentityServer4. In my configuration I have CookiePolicyOptions which contains the next setup:
For some reason, I can't reach the code from the OnAppendCooie and OnDeleteCookie.
I've also added a custom filter to append a new cookie to the response. The behavior was still the same - OnAppendCookie and OnDeleteCookie were not called.
services.Configure<CookiePolicyOptions>(
option =>
{
option.MinimumSameSitePolicy = SameSiteMode.None;
option.HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always;
option.OnAppendCookie = cookieContext =>
{
Console.WriteLine("Cookie appended: " + cookieContext.CookieName);
};
option.OnDeleteCookie = context =>
{
Console.WriteLine("Cookie deleted: " + context.CookieName);
};
});
For some reason, I can't reach the code from the OnAppendCooie and OnDeleteCookie.
I've also added a custom filter to append a new cookie to the response. The behavior was still the same - OnAppendCookie and OnDeleteCookie were not called.
Nnobody.cs11/3/2022
My filter looks like this
and registration
public class MyCustomFilter : ResultFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
context.HttpContext.Response.Cookies.Append("MyCustomCookie", "Blah-Blah");
base.OnResultExecuting(context);
}
}
and registration
services.AddControllersWithViews(c =>
{
c.Filters.Add(new MyCustomFilter());
});

Nnobody.cs11/3/2022
Am I missing something? I thought it should trigger when every cookie appends
Nnobody.cs11/3/2022
The example application
https://github.com/ihordyrman/CookieTest/blob/main/Program.cs
https://github.com/ihordyrman/CookieTest/blob/main/Program.cs
Nnobody.cs11/3/2022
Alright, I found the reason.
It requires an additional middleware.
It requires an additional middleware.
app.UseCookiePolicy();
Nnobody.cs11/3/2022
