// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Session; namespace Microsoft.AspNetCore.Builder { /// /// Represents the session state options for the application. /// public class SessionOptions { private CookieBuilder _cookieBuilder = new SessionCookieBuilder(); /// /// Determines the settings used to create the cookie. /// /// defaults to . /// defaults to . /// defaults to . /// defaults to true /// /// public CookieBuilder Cookie { get => _cookieBuilder; set => _cookieBuilder = value ?? throw new ArgumentNullException(nameof(value)); } /// /// The IdleTimeout indicates how long the session can be idle before its contents are abandoned. Each session access /// resets the timeout. Note this only applies to the content of the session, not the cookie. /// public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromMinutes(20); #region Obsolete API /// /// /// This property is obsolete and will be removed in a future version. The recommended alternative is on . /// /// /// Determines the cookie name used to persist the session ID. /// /// [Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Name) + ".")] public string CookieName { get => Cookie.Name; set => Cookie.Name = value; } /// /// /// This property is obsolete and will be removed in a future version. The recommended alternative is on . /// /// /// Determines the domain used to create the cookie. Is not provided by default. /// /// [Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Domain) + ".")] public string CookieDomain { get => Cookie.Domain; set => Cookie.Domain = value; } /// /// /// This property is obsolete and will be removed in a future version. The recommended alternative is on . /// /// /// Determines the path used to create the cookie. /// Defaults to . /// /// [Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Path) + ".")] public string CookiePath { get => Cookie.Path; set => Cookie.Path = value; } /// /// /// This property is obsolete and will be removed in a future version. The recommended alternative is on . /// /// /// Determines if the browser should allow the cookie to be accessed by client-side JavaScript. The /// default is true, which means the cookie will only be passed to HTTP requests and is not made available /// to script on the page. /// /// [Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.HttpOnly) + ".")] public bool CookieHttpOnly { get => Cookie.HttpOnly; set => Cookie.HttpOnly = value; } /// /// /// This property is obsolete and will be removed in a future version. The recommended alternative is on . /// /// /// Determines if the cookie should only be transmitted on HTTPS requests. /// /// [Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.SecurePolicy) + ".")] public CookieSecurePolicy CookieSecure { get => Cookie.SecurePolicy; set => Cookie.SecurePolicy = value; } #endregion private class SessionCookieBuilder : CookieBuilder { public SessionCookieBuilder() { Name = SessionDefaults.CookieName; Path = SessionDefaults.CookiePath; SecurePolicy = CookieSecurePolicy.None; SameSite = SameSiteMode.Lax; HttpOnly = true; } public override TimeSpan? Expiration { get => null; set => throw new InvalidOperationException(nameof(Expiration) + " cannot be set for the cookie defined by " + nameof(SessionOptions)); } } } }