|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | + |
| 6 | +namespace Microsoft.AspNetCore.Http |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Defines settings used to create a cookie. |
| 10 | + /// </summary> |
| 11 | + public class CookieBuilder |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// The name of the cookie. |
| 15 | + /// </summary> |
| 16 | + public virtual string Name { get; set; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// The cookie path. |
| 20 | + /// </summary> |
| 21 | + /// <remarks> |
| 22 | + /// Determines the value that will set on <seealso cref="CookieOptions.Path"/>. |
| 23 | + /// </remarks> |
| 24 | + public virtual string Path { get; set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// The domain to associate the cookie with. |
| 28 | + /// </summary> |
| 29 | + /// <remarks> |
| 30 | + /// Determines the value that will set on <seealso cref="CookieOptions.Domain"/>. |
| 31 | + /// </remarks> |
| 32 | + public virtual string Domain { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Indicates whether a cookie is accessible by client-side script. |
| 36 | + /// </summary> |
| 37 | + /// <remarks> |
| 38 | + /// Determines the value that will set on <seealso cref="CookieOptions.HttpOnly"/>. |
| 39 | + /// </remarks> |
| 40 | + public virtual bool HttpOnly { get; set; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// The SameSite attribute of the cookie. The default value is <see cref="SameSiteMode.Lax"/> |
| 44 | + /// </summary> |
| 45 | + /// <remarks> |
| 46 | + /// Determines the value that will set on <seealso cref="CookieOptions.SameSite"/>. |
| 47 | + /// </remarks> |
| 48 | + public virtual SameSiteMode SameSite { get; set; } = SameSiteMode.Lax; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// The policy that will be used to determine <seealso cref="CookieOptions.Secure"/>. |
| 52 | + /// This is determined from the <see cref="HttpContext"/> passed to <see cref="Build(HttpContext)"/>. |
| 53 | + /// </summary> |
| 54 | + public virtual CookieSecurePolicy SecurePolicy { get; set; } |
| 55 | + |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Gets or sets the lifespan of a cookie. |
| 59 | + /// </summary> |
| 60 | + public virtual TimeSpan? Expiration { get; set; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Creates the cookie options from the given <paramref name="context"/>. |
| 64 | + /// </summary> |
| 65 | + /// <param name="context">The <see cref="HttpContext"/>.</param> |
| 66 | + /// <returns>The cookie options.</returns> |
| 67 | + public virtual CookieOptions Build(HttpContext context) => Build(context, DateTimeOffset.Now); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Creates the cookie options from the given <paramref name="context"/> with an expiration based on <paramref name="expiresFrom"/> and <see cref="Expiration"/>. |
| 71 | + /// </summary> |
| 72 | + /// <param name="context">The <see cref="HttpContext"/>.</param> |
| 73 | + /// <param name="expiresFrom">The time to use as the base for computing <seealso cref="CookieOptions.Expires" />.</param> |
| 74 | + /// <returns>The cookie options.</returns> |
| 75 | + public virtual CookieOptions Build(HttpContext context, DateTimeOffset expiresFrom) |
| 76 | + { |
| 77 | + if (context == null) |
| 78 | + { |
| 79 | + throw new ArgumentNullException(nameof(context)); |
| 80 | + } |
| 81 | + |
| 82 | + return new CookieOptions |
| 83 | + { |
| 84 | + Path = Path ?? "/", |
| 85 | + SameSite = SameSite, |
| 86 | + HttpOnly = HttpOnly, |
| 87 | + Domain = Domain, |
| 88 | + Secure = SecurePolicy == CookieSecurePolicy.Always || (SecurePolicy == CookieSecurePolicy.SameAsRequest && context.Request.IsHttps), |
| 89 | + Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.Value) : default(DateTimeOffset?) |
| 90 | + }; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments