// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. 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.Http.Authentication { /// /// 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 RefreshKey = ".refresh"; internal const string UtcDateTimeFormat = "r"; /// /// Initializes a new instance of the class /// public AuthenticationProperties() : this(items: null) { } /// /// Initializes a new instance of the class /// /// public AuthenticationProperties(IDictionary items) { Items = items ?? new Dictionary(StringComparer.Ordinal); } /// /// State values about the authentication session. /// public IDictionary Items { get; private set; } /// /// Gets or sets whether the authentication session is persisted across multiple requests. /// public bool IsPersistent { get { return Items.ContainsKey(IsPersistentKey); } set { if (Items.ContainsKey(IsPersistentKey)) { if (!value) { Items.Remove(IsPersistentKey); } } else { if (value) { Items.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 Items.TryGetValue(RedirectUriKey, out value) ? value : null; } set { if (value != null) { Items[RedirectUriKey] = value; } else { if (Items.ContainsKey(RedirectUriKey)) { Items.Remove(RedirectUriKey); } } } } /// /// Gets or sets the time at which the authentication ticket was issued. /// public DateTimeOffset? IssuedUtc { get { string value; if (Items.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) { Items[IssuedUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture); } else { if (Items.ContainsKey(IssuedUtcKey)) { Items.Remove(IssuedUtcKey); } } } } /// /// Gets or sets the time at which the authentication ticket expires. /// public DateTimeOffset? ExpiresUtc { get { string value; if (Items.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) { Items[ExpiresUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture); } else { if (Items.ContainsKey(ExpiresUtcKey)) { Items.Remove(ExpiresUtcKey); } } } } /// /// Gets or sets if refreshing the authentication session should be allowed. /// public bool? AllowRefresh { get { string value; if (Items.TryGetValue(RefreshKey, out value)) { bool refresh; if (bool.TryParse(value, out refresh)) { return refresh; } } return null; } set { if (value.HasValue) { Items[RefreshKey] = value.Value.ToString(); } else { if (Items.ContainsKey(RefreshKey)) { Items.Remove(RefreshKey); } } } } } }