Exemple #1
0
        public async Task <bool> AddRefreshToken(OpenAPIRefreshToken token)
        {
            var exist_Token = ctx.OpenAPIRefreshTokens.Where(r => r.Subject == token.Subject & r.ClientId == token.ClientId).SingleOrDefault();

            if (exist_Token != null)
            {
                var res = await RemoveRefreshToken(exist_Token);
            }

            ctx.OpenAPIRefreshTokens.Add(token);
            return(await ctx.SaveChangesAsync() > 0);
        }
Exemple #2
0
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var clientid = context.Ticket.Properties.Dictionary["client_id"];

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

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

            using (AuthenticationRepository _repo = new AuthenticationRepository())
            {
                var refreshTokenLifeSpan = context.OwinContext.Get <string>("clientRefreshTokenLifeSpan");

                var token = new OpenAPIRefreshToken()
                {
                    Id         = Hasher.GetHash(refreshTokenId),
                    ClientId   = clientid,
                    Subject    = context.Ticket.Identity.Name,
                    IssuedUTC  = DateTime.UtcNow,
                    ExpiresUTC = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeSpan))
                };

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

                token.Protected_Ticket = context.SerializeTicket();

                var result = await _repo.AddRefreshToken(token);

                if (result)
                {
                    context.SetToken(refreshTokenId);
                }
            }
        }
Exemple #3
0
 public async Task <bool> RemoveRefreshToken(OpenAPIRefreshToken refreshToken)
 {
     ctx.OpenAPIRefreshTokens.Remove(refreshToken);
     return(await ctx.SaveChangesAsync() > 0);
 }