예제 #1
0
        public async Task<bool> AddRefreshToken(RefreshToken token)
        {
            var existingToken = this.databaseContext.RefreshTokens.SingleOrDefault(r => r.Subject == token.Subject && r.ClientId == token.ClientId);

            if (existingToken != null)
            {
                await this.RemoveRefreshToken(existingToken);
            }

            this.databaseContext.RefreshTokens.Add(token);

            return await this.databaseContext.SaveChangesAsync() > 0;
        }
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var cliendId = context.Ticket.Properties.Dictionary["as:client_id"];

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

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

            using (AuthRepository repository = new AuthRepository())
            {
                var refreshTokenLifeTime = context.OwinContext.Get<string>("as:clientRefreshTokenLifeTime");

                var token = new RefreshToken()
                {
                    Id = Helper.GetHash(refreshTokenId),
                    ClientId = cliendId,
                    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();

                var result = await repository.AddRefreshToken(token);

                if (result)
                {
                    context.SetToken(refreshTokenId);
                }
            }
        }
예제 #3
0
        public async Task<bool> RemoveRefreshToken(RefreshToken refreshToken)
        {
            this.databaseContext.RefreshTokens.Remove(refreshToken);

            return await this.databaseContext.SaveChangesAsync() > 0;
        }