How can I check for a response cookie in Asp.net Core MVC (aka Asp.Net 5 RC1)?

ID : 274467

viewed : 42

Tags : asp.net-core-mvcasp.net-core-mvc





Top 5 Answer for How can I check for a response cookie in Asp.net Core MVC (aka Asp.Net 5 RC1)?

vote vote

90

There is an extension method available in Microsoft.AspNetCore.Http.Extensions called GetTypedHeaders(). This can be called on HttpContext.Response to read Set-Cookie headers. For example in middleware perhaps we want to intercept a Set-Cookie response header and replace it:

  public async Task Invoke(HttpContext httpContext)         {             httpContext.Response.OnStarting(state =>             {                 var context = (HttpContext)state;                  var setCookieHeaders = context.Response.GetTypedHeaders().SetCookie;                  // We assume only one cookie is found. You could loop over multiple matches instead.                 // setCookieHeaders may be null if response doesn't contain set-cookie headers                 var cookie = setCookieHeaders?.FirstOrDefault(x => x.Name == "mycookiename");                  if (cookie == null)                 {                     return Task.CompletedTask;                 }                  var opts = new CookieOptions                 {                     HttpOnly = true,                     Expires = DateTimeOffset.Now.AddHours(12),                     SameSite = SameSiteMode.Lax,                     Secure = true                 };                  context.Response.Cookies.Delete(cookie.Name.Value);                 context.Response.Cookies.Append(cookie.Name.Value, "mynewcookievalue", opts);                                  return Task.CompletedTask;              }, httpContext);              await _next(httpContext);         } 
vote vote

84

Here's how I get the value of a cookie from a Response. Something like this could be used to get the whole cookie if required:

string GetCookieValueFromResponse(HttpResponse response, string cookieName) {   foreach (var headers in response.Headers.Values)     foreach (var header in headers)       if (header.StartsWith($"{cookieName}="))       {         var p1 = header.IndexOf('=');         var p2 = header.IndexOf(';');         return header.Substring(p1 + 1, p2 - p1 - 1);       }   return null; } 
vote vote

79

There is an obvious problem with Shaun's answer: it will match any HTTP-header having matching value. The idea should be to match only cookies.

A minor change like this should do the trick:

string GetCookieValueFromResponse(HttpResponse response, string cookieName) {   foreach (var headers in response.Headers)   {     if (headers.Key != "Set-Cookie")       continue;     string header = headers.Value;     if (header.StartsWith($"{cookieName}="))     {       var p1 = header.IndexOf('=');       var p2 = header.IndexOf(';');       return header.Substring(p1 + 1, p2 - p1 - 1);     }   }   return null; } 

Now the checking for a cookie name will target only actual cookie-headers.

vote vote

64

You can use this function to get full information about the cookie set value

private SetCookieHeaderValue GetCookieValueFromResponse(HttpResponse response, string cookieName) {         var cookieSetHeader = response.GetTypedHeaders().SetCookie;         if (cookieSetHeader != null)         {             var setCookie = cookieSetHeader.FirstOrDefault(x => x.Name == cookieName);             return setCookie;         }         return null; } 
vote vote

55

In my case I had to get only value of auth= and don't care about other values, but you can easy rewrite code to get dict of cookie values.

I wrote this extension for HttpResponseMessage, in order to avoid work with index and substring, I separate string to array and then cast it to dictionary

    internal static string GetCookieValueFromResponse(this HttpResponseMessage httpResponse, string cookieName)     {          foreach (var cookieStr in httpResponse.Headers.GetValues("Set-Cookie"))         {             if(string.IsNullOrEmpty(cookieStr))                 continue;              var array = cookieStr.Split(';')                 .Where(x => x.Contains('=')).Select(x => x.Trim());              var dict = array.Select(item => item.Split(new[] {'='}, 2)).ToDictionary(s => s[0], s => s[1]);               if (dict.ContainsKey(cookieName))                 return dict[cookieName];         }          return null;     } 

Top 3 video Explaining How can I check for a response cookie in Asp.net Core MVC (aka Asp.Net 5 RC1)?







Related QUESTION?