Base solution for your next web application
Open Closed

invalid_grant when Abp.TenantId is null #8881


User avatar
0
SASIMEXICO created

Hi,

I need to connect as Host to get the Tenant that are defined. But when I pass the "Abp.TenantId" as null in the Header of the HttpClient, the TokenResponse gives me the error of invalid_grant. If "Abp.TenantId" is not null, I don't have problem. In BackEnd (server), the version of Abp is 5.5.0.

In this EndPoint, I'm using a IdentityModel 4.2.0

private async Task<TokenResponse> RequestToken()
        {
            int retryCount = 0;
            TokenResponse res = null;

            bool isTokenEmpty = true;
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var disco = await client.GetDiscoveryDocumentAsync(_tokenEndpoint.TrimEnd('/'));
                    if (disco.IsError)
                    {
                        throw new Exception(disco.Error);
                    }
                    client.DefaultRequestHeaders.Add("Abp.TenantId", string.IsNullOrEmpty(_tenantId.ToString()) ? null : _tenantId.ToString());  //Set TenantId
                    do
                    {
                        retryCount++;
                        try
                        {
                            // request token
                            var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
                            {
                                Address = disco.TokenEndpoint,
                                ClientId = "client",
                                ClientSecret = _secretToken,

                                UserName = _userToken,
                                Password = _passToken,
                                //TODO: passar-ho al config
                                Scope = "default-api"
                            });

                            if (tokenResponse.IsError)
                            {
                                _log.Error(tokenResponse.Error);
                            }
                            else
                                isTokenEmpty = false;

                            _log.Debug(tokenResponse.Json);
                            res = tokenResponse;
                        }
                        catch (Exception ex)
                        {
                            Exception inner = ex;
                            while (inner.InnerException != null)
                            {
                                inner = inner.InnerException;
                            };

                            _log.DebugFormat("Error requesting credentials: {1}, Retrying={0}", retryCount, inner.Message);
                            Thread.Sleep(5000);
                        }
                    } while (isTokenEmpty && retryCount <= 2);
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
            }
            return res;
        }

Thanks for all Regards.


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @SASIMEXICO,

    Sorry for the lare reply. Instead of sending null , don't add the Abp.TenantId header for host users.

  • User Avatar
    0
    SASIMEXICO created

    Thanks for the reply.

    Regards