/// <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);
        }
    }
예제 #2
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);
    }