/// <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] <== Key with data // /IdentityServer/OpenId/users/[username] <== 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); }
/// <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_", ""))); }