Ejemplo n.º 1
0
        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>
        /// The <see cref="Task" />.
        /// </returns>
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            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 RefreshTokenDto
                            {
                                Id = DbHelper.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();

            var result = await this.authRepository.AddRefreshToken(token);

            if (result)
            {
                context.SetToken(refreshTokenId);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Removes the refresh token.
 /// </summary>
 /// <param name="refreshToken">The refresh token.</param>
 /// <returns>
 /// Identity object
 /// </returns>
 public async Task<bool> RemoveRefreshToken(RefreshTokenDto refreshToken)
 {
     this.refreshToken.Delete(x => x.Id == refreshToken.Id);
     return this.refreshToken.Commit();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds the refresh token.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns>
        /// Identity object
        /// </returns>
        public async Task<bool> AddRefreshToken(RefreshTokenDto token)
        {
            var existingToken = this.refreshToken.Find(r => r.Subject == token.Subject && r.ClientId == token.ClientId).SingleOrDefault();

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

            var newToken = this.mapperFactory.GetMapper<RefreshTokenDto, RefreshAuthTokens>().Map(token);

            this.refreshToken.Insert(newToken);

            return this.refreshToken.Commit();
        }