public override async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var guid = Guid.NewGuid().ToString();

            var clientid = context.OwinContext.Get<string>("tm:client_id");

            var refreshTokenLifeTime = context.OwinContext.Get<string>("tm:clientRefreshTokenLifeTime");

            var token = new RefreshToken()
            {
                ID = HashingService.MakeHash(guid),
                client_id = clientid,
                username = context.Ticket.Identity.Name,
                issued = DateTime.UtcNow,
                expired = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
            };

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

            token.ProtectedTicket = context.SerializeTicket();
 
            RefreshTokenService rts = new RefreshTokenService();
            var result = await rts.AddRefreshToken(token);

            if (result)
            {
                context.SetToken(guid);
            }
        }
Esempio n. 2
0
        public async Task<bool> AddRefreshToken(RefreshToken token)
        {

            var existingToken = _ctx.RefreshTokens.Where(r => r.username == token.username && r.client_id == token.client_id).SingleOrDefault();

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

            _ctx.RefreshTokens.Add(token);

            return await _ctx.SaveChangesAsync() > 0;
        }
Esempio n. 3
0
 public async Task<bool> RemoveRefreshToken(RefreshToken refreshToken)
 {
     _ctx.RefreshTokens.Remove(refreshToken);
     return await _ctx.SaveChangesAsync() > 0;
 }