Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

CookieOptions doesn't have MaxAge property #688

Closed
themacguffinman opened this issue Aug 15, 2016 · 4 comments
Closed

CookieOptions doesn't have MaxAge property #688

themacguffinman opened this issue Aug 15, 2016 · 4 comments

Comments

@themacguffinman
Copy link

themacguffinman commented Aug 15, 2016

Even though SetCookieHeaderValue supports it, CookieOptions does not have a MaxAge property which means that there is no way to append cookies with a Max-Age.

@Tratcher
Copy link
Member

Interesting, you're the first to ask for it.

Note you can use SetCookieHeaderValue and add the header directly.

@themacguffinman
Copy link
Author

themacguffinman commented Aug 15, 2016

Thanks @Tratcher.

For anyone reading this, here's a quick extension method that uses SetCookieHeaderValue directly:

public static class HttpExtensions
{
    public static void AppendCookie(this IHeaderDictionary headers, SetCookieHeaderValue cookie)
    {
        headers[HeaderNames.SetCookie] = StringValues.Concat(headers[HeaderNames.SetCookie], cookie.ToString());
    }
}

Usage:

Response.Headers.AppendCookie(new SetCookieHeaderValue("usid", String.Empty)
{
    Path = "/",
    HttpOnly = true,
    MaxAge = TimeSpan.Zero,
});

Remember to Uri.EscapeDataString() the cookie name and value if it is potentially unsafe.

Now that I think about it though, why isn't this the official method of adding cookies? CookieOptions seems a little redundant.

@muratg muratg added this to the Backlog milestone Sep 7, 2016
@muratg
Copy link

muratg commented Sep 7, 2016

Thanks for the recommendation. We'll consider post 1.1. Feel free to send a PR :)

@Tratcher Tratcher removed this from the Backlog milestone Jul 9, 2017
@jkotalik
Copy link
Contributor

We may also have to modify CookieBuilder.cs to accommodate this change.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants