diff --git a/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs b/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs index ff51ae38c7348..ee319ca338d5f 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs @@ -585,14 +585,17 @@ public string Port } set { - m_port_implicit = false; if (string.IsNullOrEmpty(value)) { // "Port" is present but has no value. + // Therefore; the effective port value is implicit. + m_port_implicit = true; m_port = string.Empty; } else { + // "Port" value is present, so we use the provided value rather than an implicit one. + m_port_implicit = false; // Parse port list if (!value.StartsWith('\"') || !value.EndsWith('\"')) { diff --git a/src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest.cs b/src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest.cs index 2f326dc7bd108..d2c44d4ef8427 100644 --- a/src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest.cs +++ b/src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest.cs @@ -349,8 +349,14 @@ public static void ToString_Compare_Success() c.Version = 0; Assert.Equal("name=value; $Path=path; $Domain=domain; $Port=\"80\"", c.ToString()); + // If a cookie string specifies either an empty string or no value for the port, then the port should be considered implicit. + // Otherwise such cookies will have no valid ports and also be incapable of assuming a usable port which in turn means they cannot be matched to ANY Uris and will effectively become nonfunctional. c.Port = ""; - Assert.Equal("name=value; $Path=path; $Domain=domain; $Port", c.ToString()); + Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString()); + + // Test null also, for sanity. + c.Port = null; + Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString()); } } }