public async Task <IClientRefreshToken> GetOrAddRefreshTokenAsync(IClient client)
        {
            var clientRefreshToken = await GetRefreshTokenAsync(client.Id)
                                     ?? await AddRefreshTokenAsync(client.Id);

            async Task <IClientRefreshToken> AddRefreshTokenAsync(int clientId)
            {
                var clientRefreshToken = new ClientRefreshToken
                {
                    ClientId = clientId,
                    Expires  = DateTime.UtcNow.AddSeconds(_envConfig.JwtRefreshExpires)
                };

                await _context.ClientRefreshTokens.AddAsync(clientRefreshToken);

                await _context.SaveChangesAsync();

                _logger.LogInformation("Added {EntityName} for ClientId={ClientId}",
                                       nameof(ClientRefreshToken),
                                       clientId);

                return(clientRefreshToken);
            }

            return(clientRefreshToken);
        }
        /// <summary>
        /// Adds the refresh token.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public bool AddRefreshToken(ClientRefreshToken token)
        {
            var dbType = Mapper.Map <ClientRefreshTokensDb>(token);

            using (var db = new CRMDb())
            {
                db.ClientRefreshTokens.Add(dbType);
                db.SaveChanges();
            }
            return(true);
        }
        public async Task <JwtTokenResponse> RefreshTokenAsync(ClientRefreshToken model)
        {
            var client = await _repository.FindByTokenAsync(model.Token, model.RefreshToken);

            if (client == null)
            {
                return(null);
            }

            return(await GenerateAndSaveTokenAsync(client));
        }
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var clientid = context.Ticket.Properties.Dictionary["as:client_id"];

            if (string.IsNullOrEmpty(clientid))
            {
                return;// Task.FromResult<object>(null);
            }

            var refreshTokenId = Guid.NewGuid().ToString("n");

            var tpaRepo = ApplicationUserManager.ThirdPartyAuthenticationRepository;

            var refreshTokenLifeTime = context.OwinContext.Get <string>("as:clientRefreshTokenLifeTime");

            var token = new ClientRefreshToken()
            {
                Id = GetHash(refreshTokenId),
                ThirdPartyClientId = clientid,
                IssuedTo           = int.Parse(context.Ticket.Identity.Claims.Where(c => c.Type == "UserID").FirstOrDefault().Value),
                IssuedOn           = DateTime.UtcNow,
                ExpiresOn          = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime)),
                LastUpdatedBy      = int.Parse(context.Ticket.Identity.Claims.Where(c => c.Type == "UserID").FirstOrDefault().Value),
                LastUpdatedOn      = DateTime.UtcNow
            };

            context.Ticket.Properties.IssuedUtc  = token.IssuedOn;
            context.Ticket.Properties.ExpiresUtc = token.ExpiresOn;

            token.ProtectedTicket = context.SerializeTicket();

            bool result = tpaRepo.AddRefreshToken(token);

            if (result)
            {
                context.SetToken(refreshTokenId);
            }

            await Task.FromResult(0);
        }
Exemple #5
0
 public async Task <ClientRefreshToken> AddAsync(ClientRefreshToken clientRefreshToken)
 {
     return(await AddEntityAsync(clientRefreshToken));
 }