views:

319

answers:

2

I'm trying to set the port on a cookie in ASP.NET (code below), but I'm getting a very unusual error at runtime (below the code). Any thoughts?

target.Cookie = new Cookie
{
    Comment = "Test Comment",
    CommentUri = new System.Uri("http://www.tempuri.org"),
    Discard = false,
    Domain = "tempuri.com",
    Expired = false,
    Expires = new DateTime(2015, 12, 31),
    HttpOnly = false,
    Name = "TestCookie",
    Path = "/",
    Port = "443",
    Secure = false,
    Value = "Test Value",
    Version = 1,
};

Exception:

System.Net.CookieException: The 'Port'='443' part of the cookie is invalid..

Edit: Sorry, I meant to mention that I also tried Port = "80" with no success.

+1  A: 

Port 443 is used for SSL connections however you have the Secure property as false. If you want to use port 443 specify Secure = true.

Brownman98
Sorry, I meant to add that I tried Port = "80" with no success, which shouldn't require Secure = true
MasterMax1313
Is there a specific reason you are setting the port of the cookie? For web requests its going to go over 80 or 443. These won't change.
Brownman98
A: 

Unfortunately I never did find a good solution to this. All I wound up doing is removing the port definition. I did notice something in the W3C spec for cookies regarding ports, and if I recall, version 2 cookies don't support the specification of a port. I've tried creating the cookie without the port, then setting the port later, but with no luck. I continue to get a runtime exception of ...threw exception: System.Net.CookieException: The 'Port'='80' part of the cookie is invalid..

Sorry this wasn't more help...

MasterMax1313