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"); using (AuthRepository _repo = new AuthRepository()) { var refreshTokenLifeTime = context.OwinContext.Get<string>("as:clientRefreshTokenLifeTime"); var token = new RefreshToken() { Id = Helper.GetHash(refreshTokenId), ClientId = clientid, Subject = context.Ticket.Identity.Name, IssuedUtc = DateTime.UtcNow, ExpiresUtc = DateTime.UtcNow.AddHours(Convert.ToDouble(refreshTokenLifeTime)) }; context.Ticket.Properties.IssuedUtc = token.IssuedUtc; context.Ticket.Properties.ExpiresUtc = token.ExpiresUtc; token.ProtectedTicket = context.SerializeTicket(); var result = await _repo.AddRefreshToken(token); if (result) { context.SetToken(refreshTokenId); } } }
public async Task<bool> AddRefreshToken(RefreshToken token) { var existingToken = RavenCnn.Session.Query<RefreshToken>().Where(t => t.ClientId == token.ClientId && t.Subject == token.Subject).FirstOrDefault(); //var existingToken = _ctx.RefreshTokens.Where(r => r.Subject == token.Subject && r.ClientId == token.ClientId).SingleOrDefault(); if (existingToken != null) { var result = await RemoveRefreshToken(existingToken); } await RavenCnn.AsyncSession.StoreAsync(token); //_ctx.RefreshTokens.Add(token); await RavenCnn.AsyncSession.SaveChangesAsync(); //return await _ctx.SaveChangesAsync() > 0; return true; }
public async Task<bool> RemoveRefreshToken(RefreshToken refreshToken) { //_ctx.RefreshTokens.Remove(refreshToken); //return await _ctx.SaveChangesAsync() > 0; RavenCnn.AsyncSession.Delete(refreshToken); await RavenCnn.AsyncSession.SaveChangesAsync(); //_ctx.RefreshTokens.Remove(refreshToken); return true; }