public Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {
            string hashedRefreshTokenId = HashUtils.Hash(context.Token);

            if (this.refreshTokenRepository.SelectAll().Any(rt => rt.RefreshTokenId == hashedRefreshTokenId))
            {
                ApplicationRefreshToken refreshToken = this.refreshTokenRepository.SelectBy(rt => rt.RefreshTokenId == hashedRefreshTokenId).First();

                context.DeserializeTicket(refreshToken.ProtectedTicket);
                this.refreshTokenRepository.Delete(refreshToken);
                this.refreshTokenRepository.SaveChanges();
            }

            return(Task.FromResult(0));
        }
        public Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            string clientId = context.Ticket.Properties.Dictionary["client_id"];

            if (!string.IsNullOrWhiteSpace(clientId))
            {
                string refreshTokenId = Guid.NewGuid().ToString();
                string refreshTokenLifeTimeInMinutes = context.OwinContext.Get <string>("refreshTokenLifeTimeInMinutes");

                ApplicationRefreshToken newRefreshToken = new ApplicationRefreshToken
                {
                    RefreshTokenId      = HashUtils.Hash(refreshTokenId),
                    Username            = context.Ticket.Identity.Name,
                    ApplicationClientId = clientId,
                    Issued  = DateTime.UtcNow,
                    Expires = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTimeInMinutes))
                };

                context.Ticket.Properties.IssuedUtc  = newRefreshToken.Issued;
                context.Ticket.Properties.ExpiresUtc = newRefreshToken.Expires;

                newRefreshToken.ProtectedTicket = context.SerializeTicket();

                //if (refreshTokenRepository.SelectAll().Any(rt => rt.Username == newRefreshToken.Username && rt.ApplicationClientId == newRefreshToken.ApplicationClientId))
                //{
                //    ApplicationRefreshToken oldRefreshToken = refreshTokenRepository.SelectBy(rt => rt.Username == newRefreshToken.Username && rt.ApplicationClientId == newRefreshToken.ApplicationClientId).First();
                //    refreshTokenRepository.Delete(oldRefreshToken);
                //}

                //refreshTokenRepository.Insert(newRefreshToken);
                //refreshTokenRepository.SaveChanges();

                context.SetToken(refreshTokenId);
            }

            return(Task.FromResult(0));
        }