public static OAuth2AuthorizationCode NewAuthCode(string authServerId)
        {
            OAuth2AuthorizationCode result = new OAuth2AuthorizationCode();

            result._authServerId = authServerId;
            return(result);
        }
        public static async Task <OAuth2AuthorizationCode> LoadAuthCodeAsync(string authCodeId, LoadAuthCodeOptions options)
        {
            if ((options & LoadAuthCodeOptions.SearchLocal) == LoadAuthCodeOptions.SearchLocal)
            {
                if (_redisClient == null)
                {
                    _redisClient = await Singletons.GetRedisClientAsync();
                }

                string fullyQualifiedAuthCodeKey = REDIS_PREFIX_OAUTH2CODE + REDIS_PREFIX_SEPARATOR + authCodeId;
                bool   localAuthCodeExists       = (await _redisClient.ExistsAsync(new string[] { fullyQualifiedAuthCodeKey }) > 0);
                if (localAuthCodeExists)
                {
                    Dictionary <string, string> authCodeDictionary = await _redisClient.HashGetAllASync <string, string, string>(fullyQualifiedAuthCodeKey);

                    long expiresInMilliseconds = await _redisClient.PttlAsync(fullyQualifiedAuthCodeKey);

                    string clientId  = authCodeDictionary.ContainsKey("client-id") ? authCodeDictionary["client-id"] : null;
                    string accountId = authCodeDictionary.ContainsKey("account-id") ? authCodeDictionary["account-id"] : null;
                    if (accountId == null)
                    {
                        return(null);
                    }
                    string userId      = authCodeDictionary.ContainsKey("user-id") ? authCodeDictionary["user-id"] : null;
                    string redirectUri = authCodeDictionary.ContainsKey("redirect-uri") ? authCodeDictionary["redirect-uri"] : null;

                    // get "is-used" value (which, when present, indicates that the authorization code has already been submitted to the token endpoint).
                    bool isUsed = authCodeDictionary.ContainsKey("is-used");
                    // if the code is used, it may also have already been assigned a token-id; we store this in case the code is compromised before it expires (i.e. and we need to revoke the token).
                    string tokenId = authCodeDictionary.ContainsKey("token-id") ? authCodeDictionary["token-id"] : null;

                    OAuth2AuthorizationCode result = new OAuth2AuthorizationCode();
                    result._id          = authCodeId;
                    result._clientId    = clientId;
                    result._accountId   = accountId;
                    result._userId      = userId;
                    result._redirectUri = redirectUri;
                    result._isUsed      = isUsed;
                    result._tokenId     = tokenId;
                    if (expiresInMilliseconds >= 0)
                    {
                        result._expirationTime = DateTimeOffset.UtcNow.AddMilliseconds(expiresInMilliseconds);
                    }

                    return(result);
                }
            }

            // valid auth code could not be found
            return(null);
        }