GetTypedHeaders().SetCookie in .net core

Karthik K V 1 Reputation point
2021-02-20T20:42:33.047+00:00

Team, I am trying to access the response cookies in a result filter - OnResultExecuted like

var setCookieHeaderValues = context.Response.GetTypedHeaders().SetCookie;

I also tried using the OnStarting delegate on context.HttpContext.Response, however it never returns the cookies which are in set-cookie rather it is always returning an empty object. Am I missing something here? Please help.

Note, that I can see the response cookies in the chrome - dev tools 1) Application - Storage - Cookies and 2) Network - Cookies - Response Cookies

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,189 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Wang-MSFT 1,051 Reputation points
    2021-02-22T08:35:38.473+00:00

    Hi, @Karthik K V ,

    You could try coding as below :

    Codes of MyActionFilterAttribute :

    public class MyActionFilterAttribute : ActionFilterAttribute  
    {  
        public override void OnResultExecuted(ResultExecutedContext filterContext)  
        {  
            var setCookieHeaderValues = filterContext.HttpContext.Response.Headers;  
    
            var cookies = setCookieHeaderValues["Set-Cookie"];  
        }  
    }  
    

    Codes of HomeController:

    [MyActionFilterAttribute]  
        public class HomeController : Controller  
        {  
      
            public HomeController()  
            {  
            }  
      
            public IActionResult Index()  
            {  
               Response.Cookies.Append("Test", "hellocookie");  
      
                return View();  
            }  
      
        }  
    

    Test result:

    70573-image.png

    ------
    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Michael Wang

    0 comments No comments