10 [global::__DynamicallyInvokable]
13 internal const int MaxSupportedVersion = 1;
15 internal const string CommentAttributeName =
"Comment";
17 internal const string CommentUrlAttributeName =
"CommentURL";
19 internal const string DiscardAttributeName =
"Discard";
21 internal const string DomainAttributeName =
"Domain";
23 internal const string ExpiresAttributeName =
"Expires";
25 internal const string MaxAgeAttributeName =
"Max-Age";
27 internal const string PathAttributeName =
"Path";
29 internal const string PortAttributeName =
"Port";
31 internal const string SecureAttributeName =
"Secure";
33 internal const string VersionAttributeName =
"Version";
35 internal const string HttpOnlyAttributeName =
"HttpOnly";
37 internal const string SeparatorLiteral =
"; ";
39 internal const string EqualsLiteral =
"=";
41 internal const string QuotesLiteral =
"\"";
43 internal const string SpecialAttributeLiteral =
"$";
45 internal static readonly
char[] PortSplitDelimiters =
new char[3]
52 internal static readonly
char[] Reserved2Name =
new char[7]
63 internal static readonly
char[] Reserved2Value =
new char[2]
69 private static Comparer staticComparer =
new Comparer();
71 private string m_comment =
string.Empty;
73 private Uri m_commentUri;
75 private CookieVariant m_cookieVariant = CookieVariant.Plain;
77 private bool m_discard;
79 private string m_domain =
string.Empty;
81 private bool m_domain_implicit =
true;
85 private string m_name =
string.Empty;
87 private string m_path =
string.Empty;
89 private bool m_path_implicit =
true;
91 private string m_port =
string.Empty;
93 private bool m_port_implicit =
true;
95 private int[] m_port_list;
97 private bool m_secure;
100 private bool m_httpOnly;
104 private string m_value =
string.Empty;
106 private int m_version;
108 private string m_domainKey =
string.Empty;
110 internal bool IsQuotedVersion;
112 internal bool IsQuotedDomain;
116 [global::__DynamicallyInvokable]
119 [global::__DynamicallyInvokable]
124 [global::__DynamicallyInvokable]
129 value =
string.Empty;
137 [global::__DynamicallyInvokable]
140 [global::__DynamicallyInvokable]
145 [global::__DynamicallyInvokable]
148 m_commentUri = value;
154 [global::__DynamicallyInvokable]
157 [global::__DynamicallyInvokable]
162 [global::__DynamicallyInvokable]
172 [global::__DynamicallyInvokable]
175 [global::__DynamicallyInvokable]
180 [global::__DynamicallyInvokable]
189 [global::__DynamicallyInvokable]
192 [global::__DynamicallyInvokable]
197 [global::__DynamicallyInvokable]
200 m_domain = ((value ==
null) ?
string.Empty : value);
201 m_domain_implicit =
false;
202 m_domainKey =
string.Empty;
206 private string _Domain
210 if (!Plain && !m_domain_implicit && m_domain.Length != 0)
212 return "$Domain=" + (IsQuotedDomain ?
"\"" :
string.Empty) + m_domain + (IsQuotedDomain ?
"\"" :
string.Empty);
218 internal bool DomainImplicit
222 return m_domain_implicit;
226 m_domain_implicit = value;
233 [global::__DynamicallyInvokable]
236 [global::__DynamicallyInvokable]
245 [global::__DynamicallyInvokable]
257 [global::__DynamicallyInvokable]
260 [global::__DynamicallyInvokable]
265 [global::__DynamicallyInvokable]
275 [global::__DynamicallyInvokable]
278 [global::__DynamicallyInvokable]
283 [global::__DynamicallyInvokable]
286 if (ValidationHelper.IsBlankString(value) || !InternalSetName(value))
288 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Name", (value ==
null) ?
"<null>" : value));
295 [global::__DynamicallyInvokable]
298 [global::__DynamicallyInvokable]
303 [global::__DynamicallyInvokable]
306 m_path = ((value ==
null) ?
string.Empty : value);
307 m_path_implicit =
false;
315 if (!Plain && !m_path_implicit && m_path.Length != 0)
317 return "$Path=" + m_path;
323 internal bool Plain => Variant == CookieVariant.Plain;
328 [global::__DynamicallyInvokable]
331 [global::__DynamicallyInvokable]
336 [global::__DynamicallyInvokable]
339 m_port_implicit =
false;
340 if (value ==
null || value.Length == 0)
342 m_port =
string.Empty;
345 if (value[0] !=
'"' || value[value.Length - 1] !=
'"')
347 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Port", value));
349 string[] array = value.Split(PortSplitDelimiters);
351 for (
int i = 0; i < array.Length; i++)
353 if (array[i] !=
string.Empty)
355 if (!
int.TryParse(array[i], out
int result))
357 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Port", value));
359 if (result < 0 || result > 65535)
361 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Port", value));
366 m_port_list = list.ToArray();
369 m_cookieVariant = CookieVariant.Rfc2965;
373 internal int[] PortList => m_port_list;
379 if (!m_port_implicit)
381 return "$Port" + ((m_port.Length == 0) ?
string.Empty : (
"=" + m_port));
390 [global::__DynamicallyInvokable]
393 [global::__DynamicallyInvokable]
398 [global::__DynamicallyInvokable]
407 [global::__DynamicallyInvokable]
410 [global::__DynamicallyInvokable]
419 [global::__DynamicallyInvokable]
422 [global::__DynamicallyInvokable]
427 [global::__DynamicallyInvokable]
430 m_value = ((value ==
null) ?
string.Empty : value);
434 internal CookieVariant Variant
438 return m_cookieVariant;
442 m_cookieVariant = value;
446 internal string DomainKey
450 if (!m_domain_implicit)
461 [global::__DynamicallyInvokable]
464 [global::__DynamicallyInvokable]
469 [global::__DynamicallyInvokable]
477 if (value > 0 && m_cookieVariant < CookieVariant.Rfc2109)
479 m_cookieVariant = CookieVariant.Rfc2109;
484 private string _Version
490 return "$Version=" + (IsQuotedVersion ?
"\"" :
string.Empty) + m_version.ToString(
NumberFormatInfo.
InvariantInfo) + (IsQuotedVersion ?
"\"" :
string.Empty);
497 [global::__DynamicallyInvokable]
506 [global::__DynamicallyInvokable]
518 [global::__DynamicallyInvokable]
519 public Cookie(
string name,
string value,
string path)
531 [global::__DynamicallyInvokable]
532 public Cookie(
string name,
string value,
string path,
string domain)
533 : this(name, value, path)
538 internal bool InternalSetName(
string value)
540 if (ValidationHelper.IsBlankString(value) || value[0] ==
'$' || value.IndexOfAny(Reserved2Name) != -1)
542 m_name =
string.Empty;
552 if (!m_port_implicit)
554 cookie.Port = m_port;
556 if (!m_path_implicit)
558 cookie.Path = m_path;
560 cookie.Domain = m_domain;
561 cookie.DomainImplicit = m_domain_implicit;
562 cookie.m_timeStamp = m_timeStamp;
563 cookie.Comment = m_comment;
564 cookie.CommentUri = m_commentUri;
565 cookie.HttpOnly = m_httpOnly;
566 cookie.Discard = m_discard;
567 cookie.Expires = m_expires;
568 cookie.Version = m_version;
569 cookie.Secure = m_secure;
570 cookie.m_cookieVariant = m_cookieVariant;
574 private static bool IsDomainEqualToHost(
string domain,
string host)
576 if (host.Length + 1 == domain.Length &&
string.Compare(host, 0, domain, 1, host.Length,
StringComparison.OrdinalIgnoreCase) == 0)
583 internal bool VerifySetDefaults(CookieVariant variant, Uri uri,
bool isLocalDomain,
string localDomain,
bool set_default,
bool isThrow)
585 string host = uri.Host;
587 string absolutePath = uri.AbsolutePath;
593 variant = CookieVariant.Plain;
595 else if (
Version == 1 && variant == CookieVariant.Unknown)
597 variant = CookieVariant.Rfc2109;
599 m_cookieVariant = variant;
601 if (m_name ==
null || m_name.Length == 0 || m_name[0] ==
'$' || m_name.IndexOfAny(Reserved2Name) != -1)
605 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Name", (m_name ==
null) ?
"<null>" : m_name));
609 if (m_value ==
null || ((m_value.Length <= 2 || m_value[0] !=
'"' || m_value[m_value.Length - 1] !=
'"') && m_value.IndexOfAny(Reserved2Value) != -1))
613 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Value", (m_value ==
null) ?
"<null>" : m_value));
621 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Comment",
Comment));
625 if (
Path !=
null && (
Path.Length <= 2 ||
Path[0] !=
'"' ||
Path[
Path.Length - 1] !=
'"') &&
Path.IndexOfAny(Reserved2Value) != -1)
629 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Path",
Path));
633 if (set_default && m_domain_implicit)
639 if (!m_domain_implicit)
641 string text = m_domain;
642 if (!DomainCharsTest(text))
646 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Domain", (text ==
null) ?
"<null>" : text));
652 if (variant != CookieVariant.Rfc2965 && variant != CookieVariant.Plain)
656 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Domain", m_domain));
662 int num = host.IndexOf(
'.');
663 if (isLocalDomain &&
string.Compare(localDomain, text,
StringComparison.OrdinalIgnoreCase) == 0)
667 else if (text.IndexOf(
'.', 1, text.Length - 2) == -1)
669 if (!IsDomainEqualToHost(text, host))
674 else if (variant == CookieVariant.Plain)
676 if (!IsDomainEqualToHost(text, host) && (host.Length <= text.Length ||
string.Compare(host, host.Length - text.Length, text, 0, text.Length,
StringComparison.OrdinalIgnoreCase) != 0))
681 else if ((num == -1 || text.Length != host.Length - num ||
string.Compare(host, num, text, 0, text.Length,
StringComparison.OrdinalIgnoreCase) != 0) && !IsDomainEqualToHost(text, host))
690 else if (
string.Compare(host, m_domain,
StringComparison.OrdinalIgnoreCase) != 0)
698 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Domain", m_domain));
703 if (set_default && m_path_implicit)
705 switch (m_cookieVariant)
707 case CookieVariant.Plain:
708 m_path = absolutePath;
710 case CookieVariant.Rfc2109:
711 m_path = absolutePath.Substring(0, absolutePath.LastIndexOf(
'/'));
714 m_path = absolutePath.Substring(0, absolutePath.LastIndexOf(
'/') + 1);
718 else if (!absolutePath.StartsWith(CookieParser.CheckQuoted(m_path)))
722 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Path", m_path));
726 if (set_default && !m_port_implicit && m_port.Length == 0)
728 m_port_list =
new int[1]
733 if (!m_port_implicit)
736 int[] port_list = m_port_list;
737 foreach (
int num2
in port_list)
749 throw new CookieException(SR.GetString(
"net_cookie_attribute",
"Port", m_port));
757 private static bool DomainCharsTest(
string name)
759 if (name ==
null || name.Length == 0)
763 foreach (
char c
in name)
765 if (c >=
'0' && c <=
'9')
801 if ((c < 'A' || c >
'Z') && c !=
'_')
811 return staticComparer;
817 [global::__DynamicallyInvokable]
818 public override bool Equals(
object comparand)
820 if (!(comparand is
Cookie))
834 [global::__DynamicallyInvokable]
842 [global::__DynamicallyInvokable]
845 string domain = _Domain;
848 string version = _Version;
849 string text = ((version.Length == 0) ?
string.Empty : (version +
"; ")) +
Name +
"=" +
Value + ((path.Length == 0) ?
string.Empty : (
"; " + path)) + ((domain.Length == 0) ?
string.Empty : (
"; " + domain)) + ((port.Length == 0) ?
string.Empty : (
"; " + port));
857 internal string ToServerString()
860 if (m_comment !=
null && m_comment.Length > 0)
862 text = text +
"; Comment=" + m_comment;
864 if (m_commentUri !=
null)
866 text = text +
"; CommentURL=\"" + m_commentUri.
ToString() +
"\"";
872 if (!m_domain_implicit && m_domain !=
null && m_domain.Length > 0)
874 text = text +
"; Domain=" + m_domain;
885 if (!m_path_implicit && m_path !=
null && m_path.Length > 0)
887 text = text +
"; Path=" + m_path;
889 if (!Plain && !m_port_implicit && m_port !=
null && m_port.Length > 0)
891 text = text +
"; Port=" + m_port;
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
override bool Equals(object comparand)
Overrides the M:System.Object.Equals(System.Object) method.
Uri CommentUri
Gets or sets a URI comment that the server can provide with a T:System.Net.Cookie.
string Port
Gets or sets a list of TCP ports that the T:System.Net.Cookie applies to.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
static readonly DateTime MinValue
Represents the smallest possible value of T:System.DateTime. This field is read-only.
string? Value
Gets or sets the P:System.Net.Cookie.Value for the T:System.Net.Cookie.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
string? Path
Gets or sets the URIs to which the T:System.Net.Cookie applies.
Cookie(string name, string value)
Initializes a new instance of the T:System.Net.Cookie class with a specified P:System....
string? Domain
Gets or sets the URI for which the T:System.Net.Cookie is valid.
bool Discard
Gets or sets the discard flag set by the server.
A type representing a date and time value.
DateTime ToLocalTime()
Converts the value of the current T:System.DateTime object to local time.
Cookie(string name, string value, string path)
Initializes a new instance of the T:System.Net.Cookie class with a specified P:System....
bool Expired
Gets or sets the current state of the T:System.Net.Cookie.
override string ToString()
Overrides the M:System.Object.ToString method.
Exposes a method that compares two objects.
bool HttpOnly
Determines whether a page script or other active content can access this cookie.
Represents the version number of an assembly, operating system, or the common language runtime....
Cookie()
Initializes a new instance of the T:System.Net.Cookie class.
string? Name
Gets or sets the name for the T:System.Net.Cookie.
DateTime TimeStamp
Gets the time when the cookie was issued as a T:System.DateTime.
bool Secure
Gets or sets the security level of a T:System.Net.Cookie.
int Version
Gets or sets the version of HTTP state maintenance to which the cookie conforms.
Cookie(string name, string value, string path, string domain)
Initializes a new instance of the T:System.Net.Cookie class with a specified P:System....
Specifies that the class can be serialized.
static DateTime Now
Gets a T:System.DateTime object that is set to the current date and time on this computer,...
DateTime Expires
Gets or sets the expiration date and time for the T:System.Net.Cookie as a T:System....
override int GetHashCode()
Overrides the M:System.Object.GetHashCode method.
Provides a set of properties and methods that are used to manage cookies. This class cannot be inheri...
Provides information about a specific culture (called a locale for unmanaged code development)....
The exception that is thrown when an error is made adding a T:System.Net.Cookie to a T:System....
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
override string ToString()
Gets a canonical string representation for the specified T:System.Uri instance.
string Comment
Gets or sets a comment that the server can add to a T:System.Net.Cookie.