Ejemplo n.º 1
0
        private async Task <string> GenerateRefreshTokenAsync(User join, ApiClient apiClient)
        {
            try
            {
                var refreshToken = await _db.ApiRefreshTokens
                                   .SingleOrDefaultAsync(token => token.ApiClientId == apiClient.Id);

                if (refreshToken == null)
                {
                    refreshToken = new ApiRefreshToken()
                    {
                        ApiClientId = apiClient.Id,
                        IssuedAt    = DateTime.UtcNow,
                        ExpiresAt   = DateTime.UtcNow.AddMinutes(apiClient.RefreshTokenLifeTimeMin),
                        Value       = (await _randomizer.GetRandomStringAsync())
                    };

                    _db.ApiRefreshTokens.Add(refreshToken);
                }
                else
                {
                    refreshToken.IssuedAt  = DateTime.UtcNow;
                    refreshToken.ExpiresAt = DateTime.UtcNow.AddMinutes(apiClient.RefreshTokenLifeTimeMin);
                    refreshToken.Value     = (await _randomizer.GetRandomStringAsync());

                    _db.ApiRefreshTokens.Update(refreshToken);
                }

                await _db.SaveChangesAsync();

                return(refreshToken.Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }