public int Delete(RefreshtokenViewModel entity)
        {
            var item = m_RefreshtokenService.GetById(entity.Id);

            if (item != null)
            {
                m_RefreshtokenService.Delete(item);
            }
            return(m_RefreshtokenService.Commit());
        }
        public int Update(RefreshtokenViewModel entity)
        {
            var item = m_RefreshtokenService.GetById(entity.Id);

            if (item != null)
            {
                return(0);
            }

            item.PROTECTEDTICKET = entity.Protectedticket;
            item.HASH            = entity.Hash;
            m_RefreshtokenService.Update(item);

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

                //if (string.IsNullOrEmpty(clientid)) throw new InvalidOperationException("AuthenticationTicket.Properties does not include audience client_id"); ;

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

                //if (audience == null) throw new InvalidOperationException("AuthenticationTicket.Properties does not include audience client_id");

                //if (audience.ResourceValues == null) throw new InvalidOperationException("AuthenticationTicket.Properties does not include audience values not set");

                //var idUser = context.Ticket.Identity.Claims.Where(c => c.Type == "Id").FirstOrDefault();
                //if (idUser == null) throw new InvalidOperationException("AuthenticationTicket.Properties does not include id user");

                //var refreshTokenLifeTime = audience.ResourceValues.FirstOrDefault().Expires;
                var idUser = context.Ticket.Identity.Claims.Where(c => c.Type == "Id").FirstOrDefault();
                var token  = new RefreshtokenViewModel()
                {
                    Userid         = idUser.Value,
                    Hash           = refreshTokenId,
                    Dtemissao      = DateTime.Now,
                    Dtemissaoutc   = DateTime.UtcNow,
                    Dtexpiracao    = DateTime.Now.AddMinutes(Convert.ToDouble(10)),
                    Dtexpiracaoutc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(10)),
                };

                context.Ticket.Properties.IssuedUtc  = DateTime.UtcNow;
                context.Ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(60);
                token.Protectedticket = context.SerializeTicket();
                var result = m_RefreshtokenBusiness.Add(token);
                if (result > 0)
                {
                    context.SetToken(refreshTokenId);
                }
                else
                {
                    throw new InvalidOperationException("Erro ao gravar informacao refresh token.");
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format("Error Serialize Ticket: {0}, innerEx: {1}", ex.Message, ex.InnerException));
            }
        }
        public int Add(RefreshtokenViewModel entity)
        {
            //var item = m_RefreshtokenService.FindBy(x=>x.USERID == entity.Userid).FirstOrDefault();
            //if (item != null) m_RefreshtokenService.Delete(item);

            m_RefreshtokenService.Add(new Data.AD_REFRESHTOKEN
            {
                PROTECTEDTICKET = entity.Protectedticket,
                HASH            = entity.Hash,
                USERID          = entity.Userid,
                DTEMISSAO       = DateTime.Now,
                DTEMISSAOUTC    = entity.Dtemissaoutc,
                DTEXPIRACAO     = entity.Dtexpiracao,
                DTEXPIRACAOUTC  = entity.Dtexpiracaoutc,
            });
            return(m_RefreshtokenService.Commit());
        }