// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace Microsoft.AspNet.Abstractions.Security { /// /// Dictionary used to store state values about the authentication session. /// public class AuthenticationProperties { internal const string IssuedUtcKey = ".issued"; internal const string ExpiresUtcKey = ".expires"; internal const string IsPersistentKey = ".persistent"; internal const string RedirectUriKey = ".redirect"; internal const string UtcDateTimeFormat = "r"; /// /// Initializes a new instance of the class /// public AuthenticationProperties() : this(dictionary: null) { } /// /// Initializes a new instance of the class /// /// public AuthenticationProperties(IDictionary dictionary) { Dictionary = dictionary ?? new Dictionary(StringComparer.Ordinal); } /// /// State values about the authentication session. /// public IDictionary Dictionary { get; private set; } /// /// Gets or sets whether the authentication session is persisted across multiple requests. /// public bool IsPersistent { get { return Dictionary.ContainsKey(IsPersistentKey); } set { if (Dictionary.ContainsKey(IsPersistentKey)) { if (!value) { Dictionary.Remove(IsPersistentKey); } } else { if (value) { Dictionary.Add(IsPersistentKey, string.Empty); } } } } /// /// Gets or sets the full path or absolute URI to be used as an http redirect response value. /// [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "By design")] public string RedirectUri { get { string value; return Dictionary.TryGetValue(RedirectUriKey, out value) ? value : null; } set { if (value != null) { Dictionary[RedirectUriKey] = value; } else { if (Dictionary.ContainsKey(RedirectUriKey)) { Dictionary.Remove(RedirectUriKey); } } } } /// /// Gets or sets the time at which the authentication ticket was issued. /// public DateTimeOffset? IssuedUtc { get { string value; if (Dictionary.TryGetValue(IssuedUtcKey, out value)) { DateTimeOffset dateTimeOffset; if (DateTimeOffset.TryParseExact(value, UtcDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset)) { return dateTimeOffset; } } return null; } set { if (value.HasValue) { Dictionary[IssuedUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture); } else { if (Dictionary.ContainsKey(IssuedUtcKey)) { Dictionary.Remove(IssuedUtcKey); } } } } /// /// Gets or sets the time at which the authentication ticket expires. /// public DateTimeOffset? ExpiresUtc { get { string value; if (Dictionary.TryGetValue(ExpiresUtcKey, out value)) { DateTimeOffset dateTimeOffset; if (DateTimeOffset.TryParseExact(value, UtcDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset)) { return dateTimeOffset; } } return null; } set { if (value.HasValue) { Dictionary[ExpiresUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture); } else { if (Dictionary.ContainsKey(ExpiresUtcKey)) { Dictionary.Remove(ExpiresUtcKey); } } } } } }