tuitio/IdentityServer.Domain.Data/Repositories/IdentityRepository.cs

55 lines
1.7 KiB
C#
Raw Normal View History

2020-12-20 00:18:53 +02:00
using IdentityServer.Domain.Data.DbContexts;
using IdentityServer.Domain.Entities;
2021-11-13 16:04:04 +02:00
using IdentityServer.Domain.Models;
2020-12-20 00:18:53 +02:00
using IdentityServer.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
2021-11-13 16:04:04 +02:00
using System;
2021-11-13 17:17:13 +02:00
using System.Linq;
2020-12-20 00:18:53 +02:00
using System.Threading.Tasks;
namespace IdentityServer.Domain.Data.Repositories
{
class IdentityRepository : IIdentityRepository
{
private readonly IdentityDbContext _dbContext;
public IdentityRepository(IdentityDbContext dbContext)
{
_dbContext = dbContext;
}
2021-11-13 16:04:04 +02:00
public Task<AppUser> GetUser(string userName, string password)
2020-12-20 00:18:53 +02:00
{
return _dbContext.Users
.Include(z => z.Status)
.Include(z => z.Claims)
.FirstOrDefaultAsync(z => z.UserName == userName && z.Password == password);
2020-12-20 00:18:53 +02:00
}
2021-11-13 16:04:04 +02:00
public async Task UpdateUserAfterAuthentication(AppUser user, Token token)
{
var userToken = new UserToken()
{
UserId = user.UserId,
Token = token.Raw,
ValidFrom = token.ValidFrom,
ValidUntil = token.ValidUntil
};
await _dbContext.AddAsync(userToken);
user.LastLoginDate = DateTime.Now;
_dbContext.Update(user);
await _dbContext.SaveChangesAsync();
}
2021-11-13 17:17:13 +02:00
public Task<UserToken[]> GetActiveTokens()
{
var currentDate = DateTime.Now;
var query = _dbContext.UserTokens
.Where(z => z.ValidFrom <= currentDate && z.ValidUntil >= currentDate && (!z.Burnt.HasValue || z.Burnt.Value == false));
return query.ToArrayAsync();
}
2020-12-20 00:18:53 +02:00
}
}