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

                if (string.IsNullOrEmpty(clientid))
                {
                    return;
                }

                var refreshTokenId = Guid.NewGuid().ToString("n");
                var refreshTokenLifeTime = context.OwinContext.Get<string>("as:clientRefreshTokenLifeTime");

                var token = new RefreshToken()
                {
                    Id = HashHelper.GetHash(refreshTokenId),
                    ClientId = clientid,
                    Subject = context.Ticket.Identity.Name,
                    IssuedUtc = DateTime.UtcNow,
                    ExpiresUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
                };

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

                token.ProtectedTicket = context.SerializeTicket();

                bool result;
                using (AuthRepository authService = new AuthRepository())
                {
                    result = authService.AddRefreshToken(token);
                }



                if (result)
                {
                    context.SetToken(refreshTokenId);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error creating token: " + ex.InnerException);
            }
        }
Esempio n. 2
0
 public async Task<bool> RemoveRefreshTokenAsync(RefreshToken refreshToken)
 {
     _dbContext.RefreshTokens.Remove(refreshToken);
     return await _dbContext.SaveChangesAsync() > 0;
 }
Esempio n. 3
0
 public bool RemoveRefreshToken(RefreshToken refreshToken)
 {
     _dbContext.RefreshTokens.Remove(refreshToken);
     return _dbContext.SaveChanges() > 0;
 }
Esempio n. 4
0
        public async Task<bool> AddRefreshTokenAsync(RefreshToken token)
        {

            var existingToken = _dbContext.RefreshTokens.FirstOrDefault(r => r.Subject == token.Subject && r.ClientId == token.ClientId);

            if (existingToken != null)
            {
                await RemoveRefreshTokenAsync(existingToken);
            }

            _dbContext.RefreshTokens.Add(token);

            return await _dbContext.SaveChangesAsync() > 0;
        }
Esempio n. 5
0
        // TODO: Move tokens into their own repo


        public bool AddRefreshToken(RefreshToken token)
        {
            if (token != null)
            {
                var existingToken = _dbContext.RefreshTokens.SingleOrDefault(r => r.Subject == token.Subject && r.ClientId == token.ClientId);

                if (existingToken != null)
                {
                    RemoveRefreshToken(existingToken);
                }

                _dbContext.RefreshTokens.Add(token);

                return _dbContext.SaveChanges() > 0;
            }
            return false;
        }