How can I check for a response cookie in Asp.net Core MVC (aka Asp.Net 5 RC1)?
I'm converting a web forms application to asp.net core mvc. In my web forms application sometimes after I set some response cookies other code needs to see if they were set, and if so, access the cookie's properties (i.e. value, Expires, Secure, http). In webforms and MVC 5 it's possible to iterate over the cookies and return any particular cookies like so (old school I know)
for(int i = 0; i < cookies.Count; i++) {
if(cookies[i].Name == cookieName) {
return cookies[i];
}
}
But the interface for accessing response cookies in asp.net core mvc looks like this:
Based on this interface I don't see a way to check to see if a response cookie exists and obtain it's properties. But there has gotta be some way to do it?
In an action method I tried setting two cookies on the response object and then immediately trying to access them. But intellisense doesn't show any methods, properties or indexers that would allow me to access them:
For a moment, I thought that perhaps I could use Response.Cookies.ToString();
and just parse the information to find my cookie info, but alas, the ToString()
call returns "Microsoft.AspNet.Http.Internal.ResponseCookies" because the object doesn't override the default implementation.
Just for fun I also checked the current dev branch of GitHub to see if the interface has changed since RC1 but it has not. So given this interface, how do I check for the existence of a response cookie and get it's properties? I've thought about trying to hack in via the response headers collection but that seems pretty lame.
asp.net-core-mvc
add a comment |
I'm converting a web forms application to asp.net core mvc. In my web forms application sometimes after I set some response cookies other code needs to see if they were set, and if so, access the cookie's properties (i.e. value, Expires, Secure, http). In webforms and MVC 5 it's possible to iterate over the cookies and return any particular cookies like so (old school I know)
for(int i = 0; i < cookies.Count; i++) {
if(cookies[i].Name == cookieName) {
return cookies[i];
}
}
But the interface for accessing response cookies in asp.net core mvc looks like this:
Based on this interface I don't see a way to check to see if a response cookie exists and obtain it's properties. But there has gotta be some way to do it?
In an action method I tried setting two cookies on the response object and then immediately trying to access them. But intellisense doesn't show any methods, properties or indexers that would allow me to access them:
For a moment, I thought that perhaps I could use Response.Cookies.ToString();
and just parse the information to find my cookie info, but alas, the ToString()
call returns "Microsoft.AspNet.Http.Internal.ResponseCookies" because the object doesn't override the default implementation.
Just for fun I also checked the current dev branch of GitHub to see if the interface has changed since RC1 but it has not. So given this interface, how do I check for the existence of a response cookie and get it's properties? I've thought about trying to hack in via the response headers collection but that seems pretty lame.
asp.net-core-mvc
Have you triedResponse.Cookies
in your action method?
– Will Ray
Apr 28 '16 at 14:21
Will I have. I will update the question to indicate that.
– Ron C
Apr 28 '16 at 14:51
Oh, sorry. I meantRequest.Cookies
, my mistake!
– Will Ray
Apr 28 '16 at 15:40
@Will No worries. Based on your suggestion I tried adding a cookie to the response cookies and then checked the request cookies to see if I could gain access to the cookie that way. While the request cookies object does provide a way to read it's cookies, it turns out that adding a cookie to the response cookies does not add the cookie to the request cookies under the hood as is done in asp.net 4.x. So that approach dose not work either.
– Ron C
Apr 28 '16 at 18:11
add a comment |
I'm converting a web forms application to asp.net core mvc. In my web forms application sometimes after I set some response cookies other code needs to see if they were set, and if so, access the cookie's properties (i.e. value, Expires, Secure, http). In webforms and MVC 5 it's possible to iterate over the cookies and return any particular cookies like so (old school I know)
for(int i = 0; i < cookies.Count; i++) {
if(cookies[i].Name == cookieName) {
return cookies[i];
}
}
But the interface for accessing response cookies in asp.net core mvc looks like this:
Based on this interface I don't see a way to check to see if a response cookie exists and obtain it's properties. But there has gotta be some way to do it?
In an action method I tried setting two cookies on the response object and then immediately trying to access them. But intellisense doesn't show any methods, properties or indexers that would allow me to access them:
For a moment, I thought that perhaps I could use Response.Cookies.ToString();
and just parse the information to find my cookie info, but alas, the ToString()
call returns "Microsoft.AspNet.Http.Internal.ResponseCookies" because the object doesn't override the default implementation.
Just for fun I also checked the current dev branch of GitHub to see if the interface has changed since RC1 but it has not. So given this interface, how do I check for the existence of a response cookie and get it's properties? I've thought about trying to hack in via the response headers collection but that seems pretty lame.
asp.net-core-mvc
I'm converting a web forms application to asp.net core mvc. In my web forms application sometimes after I set some response cookies other code needs to see if they were set, and if so, access the cookie's properties (i.e. value, Expires, Secure, http). In webforms and MVC 5 it's possible to iterate over the cookies and return any particular cookies like so (old school I know)
for(int i = 0; i < cookies.Count; i++) {
if(cookies[i].Name == cookieName) {
return cookies[i];
}
}
But the interface for accessing response cookies in asp.net core mvc looks like this:
Based on this interface I don't see a way to check to see if a response cookie exists and obtain it's properties. But there has gotta be some way to do it?
In an action method I tried setting two cookies on the response object and then immediately trying to access them. But intellisense doesn't show any methods, properties or indexers that would allow me to access them:
For a moment, I thought that perhaps I could use Response.Cookies.ToString();
and just parse the information to find my cookie info, but alas, the ToString()
call returns "Microsoft.AspNet.Http.Internal.ResponseCookies" because the object doesn't override the default implementation.
Just for fun I also checked the current dev branch of GitHub to see if the interface has changed since RC1 but it has not. So given this interface, how do I check for the existence of a response cookie and get it's properties? I've thought about trying to hack in via the response headers collection but that seems pretty lame.
asp.net-core-mvc
asp.net-core-mvc
edited Apr 28 '16 at 14:58
Ron C
asked Apr 27 '16 at 20:07


Ron CRon C
10.1k64075
10.1k64075
Have you triedResponse.Cookies
in your action method?
– Will Ray
Apr 28 '16 at 14:21
Will I have. I will update the question to indicate that.
– Ron C
Apr 28 '16 at 14:51
Oh, sorry. I meantRequest.Cookies
, my mistake!
– Will Ray
Apr 28 '16 at 15:40
@Will No worries. Based on your suggestion I tried adding a cookie to the response cookies and then checked the request cookies to see if I could gain access to the cookie that way. While the request cookies object does provide a way to read it's cookies, it turns out that adding a cookie to the response cookies does not add the cookie to the request cookies under the hood as is done in asp.net 4.x. So that approach dose not work either.
– Ron C
Apr 28 '16 at 18:11
add a comment |
Have you triedResponse.Cookies
in your action method?
– Will Ray
Apr 28 '16 at 14:21
Will I have. I will update the question to indicate that.
– Ron C
Apr 28 '16 at 14:51
Oh, sorry. I meantRequest.Cookies
, my mistake!
– Will Ray
Apr 28 '16 at 15:40
@Will No worries. Based on your suggestion I tried adding a cookie to the response cookies and then checked the request cookies to see if I could gain access to the cookie that way. While the request cookies object does provide a way to read it's cookies, it turns out that adding a cookie to the response cookies does not add the cookie to the request cookies under the hood as is done in asp.net 4.x. So that approach dose not work either.
– Ron C
Apr 28 '16 at 18:11
Have you tried
Response.Cookies
in your action method?– Will Ray
Apr 28 '16 at 14:21
Have you tried
Response.Cookies
in your action method?– Will Ray
Apr 28 '16 at 14:21
Will I have. I will update the question to indicate that.
– Ron C
Apr 28 '16 at 14:51
Will I have. I will update the question to indicate that.
– Ron C
Apr 28 '16 at 14:51
Oh, sorry. I meant
Request.Cookies
, my mistake!– Will Ray
Apr 28 '16 at 15:40
Oh, sorry. I meant
Request.Cookies
, my mistake!– Will Ray
Apr 28 '16 at 15:40
@Will No worries. Based on your suggestion I tried adding a cookie to the response cookies and then checked the request cookies to see if I could gain access to the cookie that way. While the request cookies object does provide a way to read it's cookies, it turns out that adding a cookie to the response cookies does not add the cookie to the request cookies under the hood as is done in asp.net 4.x. So that approach dose not work either.
– Ron C
Apr 28 '16 at 18:11
@Will No worries. Based on your suggestion I tried adding a cookie to the response cookies and then checked the request cookies to see if I could gain access to the cookie that way. While the request cookies object does provide a way to read it's cookies, it turns out that adding a cookie to the response cookies does not add the cookie to the request cookies under the hood as is done in asp.net 4.x. So that approach dose not work either.
– Ron C
Apr 28 '16 at 18:11
add a comment |
4 Answers
4
active
oldest
votes
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;
}
add a comment |
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.
1
Interesting catch. I also now see Shaun is using a loop in a loop which I also don't think is right. I guess I just took from his answer that the value needed to be looped through which I ultimately did. I checked my code and I check thatheader.Key == "Set-Cookie"
which I think is probably right rather thanCookie
– Ron C
Sep 19 '18 at 15:42
My bad, I looked closer at my code, the two loops are required.
– Ron C
Sep 19 '18 at 15:44
Ron's comment is correct. It isSet-Cookie
on response.Cookie
on request.
– Jari Turkia
Sep 20 '18 at 5:44
add a comment |
The snippet seems to work for me. The result for me (derived from Shaun and Ron) looks like:
public static string GetCookieValueFromResponse(HttpResponse response, string cookieName)
{
// inspect the cookies in HttpResponse.
string match = $"{cookieName}=";
var p1 = match.Length;
foreach (var headers in response.Headers)
{
if (headers.Key != "Set-Cookie")
continue;
foreach (string header in headers.Value)
{
if (header.StartsWith(match) && header.Length > p1 && header[p1] != ';')
{
var p2 = header.IndexOf(';', p1);
return header.Substring(p1 + 1, p2 - p1 - 1);
}
}
}
return null;
}
add a comment |
In cases where multiple "Set-Cookie" headers exists the last one should be used:
private string GetCookieValueFromResponse(Microsoft.AspNetCore.Http.HttpResponse response, string cookieName)
{
var value = string.Empty;
foreach (var header in response.Headers["Set-Cookie"])
{
if (!header.Trim().StartsWith($"{cookieName}="))
continue;
var p1 = header.IndexOf('=');
var p2 = header.IndexOf(';');
value = header.Substring(p1 + 1, p2 - p1 - 1);
}
return value;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36899875%2fhow-can-i-check-for-a-response-cookie-in-asp-net-core-mvc-aka-asp-net-5-rc1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Jun 12 '18 at 15:18


Shaun Bowe
6,134104365
6,134104365
answered Jun 7 '17 at 4:33
TreeAndLeafTreeAndLeaf
4301710
4301710
add a comment |
add a comment |
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.
1
Interesting catch. I also now see Shaun is using a loop in a loop which I also don't think is right. I guess I just took from his answer that the value needed to be looped through which I ultimately did. I checked my code and I check thatheader.Key == "Set-Cookie"
which I think is probably right rather thanCookie
– Ron C
Sep 19 '18 at 15:42
My bad, I looked closer at my code, the two loops are required.
– Ron C
Sep 19 '18 at 15:44
Ron's comment is correct. It isSet-Cookie
on response.Cookie
on request.
– Jari Turkia
Sep 20 '18 at 5:44
add a comment |
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.
1
Interesting catch. I also now see Shaun is using a loop in a loop which I also don't think is right. I guess I just took from his answer that the value needed to be looped through which I ultimately did. I checked my code and I check thatheader.Key == "Set-Cookie"
which I think is probably right rather thanCookie
– Ron C
Sep 19 '18 at 15:42
My bad, I looked closer at my code, the two loops are required.
– Ron C
Sep 19 '18 at 15:44
Ron's comment is correct. It isSet-Cookie
on response.Cookie
on request.
– Jari Turkia
Sep 20 '18 at 5:44
add a comment |
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.
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.
edited Sep 20 '18 at 5:45
answered Sep 19 '18 at 15:26


Jari TurkiaJari Turkia
3281711
3281711
1
Interesting catch. I also now see Shaun is using a loop in a loop which I also don't think is right. I guess I just took from his answer that the value needed to be looped through which I ultimately did. I checked my code and I check thatheader.Key == "Set-Cookie"
which I think is probably right rather thanCookie
– Ron C
Sep 19 '18 at 15:42
My bad, I looked closer at my code, the two loops are required.
– Ron C
Sep 19 '18 at 15:44
Ron's comment is correct. It isSet-Cookie
on response.Cookie
on request.
– Jari Turkia
Sep 20 '18 at 5:44
add a comment |
1
Interesting catch. I also now see Shaun is using a loop in a loop which I also don't think is right. I guess I just took from his answer that the value needed to be looped through which I ultimately did. I checked my code and I check thatheader.Key == "Set-Cookie"
which I think is probably right rather thanCookie
– Ron C
Sep 19 '18 at 15:42
My bad, I looked closer at my code, the two loops are required.
– Ron C
Sep 19 '18 at 15:44
Ron's comment is correct. It isSet-Cookie
on response.Cookie
on request.
– Jari Turkia
Sep 20 '18 at 5:44
1
1
Interesting catch. I also now see Shaun is using a loop in a loop which I also don't think is right. I guess I just took from his answer that the value needed to be looped through which I ultimately did. I checked my code and I check that
header.Key == "Set-Cookie"
which I think is probably right rather than Cookie
– Ron C
Sep 19 '18 at 15:42
Interesting catch. I also now see Shaun is using a loop in a loop which I also don't think is right. I guess I just took from his answer that the value needed to be looped through which I ultimately did. I checked my code and I check that
header.Key == "Set-Cookie"
which I think is probably right rather than Cookie
– Ron C
Sep 19 '18 at 15:42
My bad, I looked closer at my code, the two loops are required.
– Ron C
Sep 19 '18 at 15:44
My bad, I looked closer at my code, the two loops are required.
– Ron C
Sep 19 '18 at 15:44
Ron's comment is correct. It is
Set-Cookie
on response. Cookie
on request.– Jari Turkia
Sep 20 '18 at 5:44
Ron's comment is correct. It is
Set-Cookie
on response. Cookie
on request.– Jari Turkia
Sep 20 '18 at 5:44
add a comment |
The snippet seems to work for me. The result for me (derived from Shaun and Ron) looks like:
public static string GetCookieValueFromResponse(HttpResponse response, string cookieName)
{
// inspect the cookies in HttpResponse.
string match = $"{cookieName}=";
var p1 = match.Length;
foreach (var headers in response.Headers)
{
if (headers.Key != "Set-Cookie")
continue;
foreach (string header in headers.Value)
{
if (header.StartsWith(match) && header.Length > p1 && header[p1] != ';')
{
var p2 = header.IndexOf(';', p1);
return header.Substring(p1 + 1, p2 - p1 - 1);
}
}
}
return null;
}
add a comment |
The snippet seems to work for me. The result for me (derived from Shaun and Ron) looks like:
public static string GetCookieValueFromResponse(HttpResponse response, string cookieName)
{
// inspect the cookies in HttpResponse.
string match = $"{cookieName}=";
var p1 = match.Length;
foreach (var headers in response.Headers)
{
if (headers.Key != "Set-Cookie")
continue;
foreach (string header in headers.Value)
{
if (header.StartsWith(match) && header.Length > p1 && header[p1] != ';')
{
var p2 = header.IndexOf(';', p1);
return header.Substring(p1 + 1, p2 - p1 - 1);
}
}
}
return null;
}
add a comment |
The snippet seems to work for me. The result for me (derived from Shaun and Ron) looks like:
public static string GetCookieValueFromResponse(HttpResponse response, string cookieName)
{
// inspect the cookies in HttpResponse.
string match = $"{cookieName}=";
var p1 = match.Length;
foreach (var headers in response.Headers)
{
if (headers.Key != "Set-Cookie")
continue;
foreach (string header in headers.Value)
{
if (header.StartsWith(match) && header.Length > p1 && header[p1] != ';')
{
var p2 = header.IndexOf(';', p1);
return header.Substring(p1 + 1, p2 - p1 - 1);
}
}
}
return null;
}
The snippet seems to work for me. The result for me (derived from Shaun and Ron) looks like:
public static string GetCookieValueFromResponse(HttpResponse response, string cookieName)
{
// inspect the cookies in HttpResponse.
string match = $"{cookieName}=";
var p1 = match.Length;
foreach (var headers in response.Headers)
{
if (headers.Key != "Set-Cookie")
continue;
foreach (string header in headers.Value)
{
if (header.StartsWith(match) && header.Length > p1 && header[p1] != ';')
{
var p2 = header.IndexOf(';', p1);
return header.Substring(p1 + 1, p2 - p1 - 1);
}
}
}
return null;
}
answered Jan 2 at 4:57
MenaceMenace
549613
549613
add a comment |
add a comment |
In cases where multiple "Set-Cookie" headers exists the last one should be used:
private string GetCookieValueFromResponse(Microsoft.AspNetCore.Http.HttpResponse response, string cookieName)
{
var value = string.Empty;
foreach (var header in response.Headers["Set-Cookie"])
{
if (!header.Trim().StartsWith($"{cookieName}="))
continue;
var p1 = header.IndexOf('=');
var p2 = header.IndexOf(';');
value = header.Substring(p1 + 1, p2 - p1 - 1);
}
return value;
}
add a comment |
In cases where multiple "Set-Cookie" headers exists the last one should be used:
private string GetCookieValueFromResponse(Microsoft.AspNetCore.Http.HttpResponse response, string cookieName)
{
var value = string.Empty;
foreach (var header in response.Headers["Set-Cookie"])
{
if (!header.Trim().StartsWith($"{cookieName}="))
continue;
var p1 = header.IndexOf('=');
var p2 = header.IndexOf(';');
value = header.Substring(p1 + 1, p2 - p1 - 1);
}
return value;
}
add a comment |
In cases where multiple "Set-Cookie" headers exists the last one should be used:
private string GetCookieValueFromResponse(Microsoft.AspNetCore.Http.HttpResponse response, string cookieName)
{
var value = string.Empty;
foreach (var header in response.Headers["Set-Cookie"])
{
if (!header.Trim().StartsWith($"{cookieName}="))
continue;
var p1 = header.IndexOf('=');
var p2 = header.IndexOf(';');
value = header.Substring(p1 + 1, p2 - p1 - 1);
}
return value;
}
In cases where multiple "Set-Cookie" headers exists the last one should be used:
private string GetCookieValueFromResponse(Microsoft.AspNetCore.Http.HttpResponse response, string cookieName)
{
var value = string.Empty;
foreach (var header in response.Headers["Set-Cookie"])
{
if (!header.Trim().StartsWith($"{cookieName}="))
continue;
var p1 = header.IndexOf('=');
var p2 = header.IndexOf(';');
value = header.Substring(p1 + 1, p2 - p1 - 1);
}
return value;
}
answered Feb 13 at 4:21
Ariel DeilAriel Deil
663
663
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36899875%2fhow-can-i-check-for-a-response-cookie-in-asp-net-core-mvc-aka-asp-net-5-rc1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried
Response.Cookies
in your action method?– Will Ray
Apr 28 '16 at 14:21
Will I have. I will update the question to indicate that.
– Ron C
Apr 28 '16 at 14:51
Oh, sorry. I meant
Request.Cookies
, my mistake!– Will Ray
Apr 28 '16 at 15:40
@Will No worries. Based on your suggestion I tried adding a cookie to the response cookies and then checked the request cookies to see if I could gain access to the cookie that way. While the request cookies object does provide a way to read it's cookies, it turns out that adding a cookie to the response cookies does not add the cookie to the request cookies under the hood as is done in asp.net 4.x. So that approach dose not work either.
– Ron C
Apr 28 '16 at 18:11