/// <summary>
        /// Finds the user by subject identifier.
        /// </summary>
        /// <param name="subjectId">The subject identifier.</param>
        /// <returns></returns>
        public IAppUser FindBySubjectId(string subjectId)
        {
            // /IdentityServer/OpenId/providers/[Provider]/userId/[userId] &lt;== Key with data
            // /IdentityServer/OpenId/users/[username] &lt;== Key forwarding to the provider+subjectId
            const string keyFormat = "IdentityServer/OpenId/subjectId/{0}";
            var          rdb       = _redis.GetDatabase();
            var          result    = rdb.StringGet(string.Format(keyFormat, subjectId));

            if (result.HasValue)
            {
                // IMPORTANT! This line might throw an exception if we change the format/version
                IAppUser foundSubjectId = JsonConvert.DeserializeObject <TUser>(result.ToString(), _jsonSerializerSettings);

                return(foundSubjectId);
            }

            // Search in the LDAP
            if (subjectId.Contains("ldap_"))
            {
                var found = _authenticationService.FindUser(subjectId.Replace("ldap_", "")); // As of now, subjectId is the same as the username

                if (found != null)
                {
                    SetRedisData(found);
                    return(found);
                }
            }

            // Not found at all
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Finds the user by subject identifier, but does not add the user to the cache
        /// since he's not logged in, in the current context.
        /// </summary>
        /// <param name="subjectId">The subject identifier.</param>
        /// <returns>The application user.</returns>
        public IAppUser FindBySubjectId(string subjectId)
        {
            // Search in external provider first
            var foundAnything = _users.Select(f => f.Value.FirstOrDefault(g => g.Value.SubjectId == subjectId)).FirstOrDefault();

            if (foundAnything.Value != null)
            {
                return(foundAnything.Value);
            }

            // Search in the LDAP
            return(_authenticationService.FindUser(subjectId.Replace("ldap_", "")));
        }