/// <summary>
        /// Adds a client to the list of clients the user has signed into during their session.
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="clientId"></param>
        public static void AddClientId(this AuthenticationProperties properties, string clientId)
        {
            if (clientId == null)
            {
                throw new ArgumentNullException(nameof(clientId));
            }

            var clients = properties.GetClientList();

            if (!clients.Contains(clientId))
            {
                var update = clients.ToList();
                update.Add(clientId);

                var value = EncodeList(update);
                if (value == null)
                {
                    properties.Items.Remove(ClientListKey);
                }
                else
                {
                    properties.Items[ClientListKey] = value;
                }
            }
        }
        /// <summary>
        /// Adds a client to the list of clients the user has signed into during their session.
        /// </summary>
        /// <param name="clientId">The client identifier.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">clientId</exception>
        public virtual async Task AddClientIdAsync(string clientId)
        {
            if (clientId == null)
            {
                throw new ArgumentNullException(nameof(clientId));
            }

            await AuthenticateAsync();

            if (Properties != null)
            {
                var clientIds = Properties.GetClientList();
                if (!clientIds.Contains(clientId))
                {
                    Properties.AddClientId(clientId);
                    await UpdateSessionCookie();
                }
            }
        }
예제 #3
0
    /// <summary>
    /// Creates a session identifier for the signin context and issues the session id cookie.
    /// </summary>
    /// <param name="principal"></param>
    /// <param name="properties"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException">
    /// principal
    /// or
    /// properties
    /// </exception>
    public virtual async Task <string> CreateSessionIdAsync(ClaimsPrincipal principal, AuthenticationProperties properties)
    {
        if (principal == null)
        {
            throw new ArgumentNullException(nameof(principal));
        }
        if (properties == null)
        {
            throw new ArgumentNullException(nameof(properties));
        }

        var currentSubjectId = (await GetUserAsync())?.GetSubjectId();
        var newSubjectId     = principal.GetSubjectId();

        if (properties.GetSessionId() == null)
        {
            var currSid = await GetSessionIdAsync();

            if (newSubjectId == currentSubjectId && currSid != null)
            {
                properties.SetSessionId(currSid);
                var clients = Properties.GetClientList();
                if (clients.Any())
                {
                    properties.SetClientList(clients);
                }
            }
            else
            {
                properties.SetSessionId(CryptoRandom.CreateUniqueId(16, CryptoRandom.OutputFormat.Hex));
            }
        }

        var sid = properties.GetSessionId();

        IssueSessionIdCookie(sid);

        Principal  = principal;
        Properties = properties;

        return(sid);
    }
    /// <summary>
    /// Adds a client to the list of clients the user has signed into during their session.
    /// </summary>
    /// <param name="properties"></param>
    /// <param name="clientId"></param>
    public static void AddClientId(this AuthenticationProperties properties, string clientId)
    {
        if (clientId == null)
        {
            throw new ArgumentNullException(nameof(clientId));
        }

        var clients = properties.GetClientList();

        if (!clients.Contains(clientId))
        {
            var update = clients.ToList();
            update.Add(clientId);

            properties.SetClientList(update);
        }
    }
예제 #5
0
        /// <summary>
        /// Gets the list of clients the user has signed into during their session.
        /// </summary>
        /// <returns></returns>
        public virtual async Task <IEnumerable <string> > GetClientListAsync()
        {
            await AuthenticateAsync();

            try
            {
                return(Properties.GetClientList());
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error decoding client list");
                // clear so we don't keep failing
                Properties.RemoveClientList();
                await UpdateSessionCookie();
            }

            return(Enumerable.Empty <string>());
        }