UpdateUserAfterAuthentication
parent
be76c3a5f3
commit
36d23aa924
|
@ -21,7 +21,7 @@ namespace IdentityServer.Application.Services
|
||||||
|
|
||||||
public async Task<Token> Authenticate(string userName, string password)
|
public async Task<Token> Authenticate(string userName, string password)
|
||||||
{
|
{
|
||||||
var user = await _identityRepository.GetAppUser(userName, password);
|
var user = await _identityRepository.GetUser(userName, password);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ namespace IdentityServer.Application.Services
|
||||||
var currentDate = DateTime.Now;
|
var currentDate = DateTime.Now;
|
||||||
var token = new Token() { Raw = tokenRaw, ValidFrom = currentDate, ValidUntil = currentDate.AddMonths(12) };
|
var token = new Token() { Raw = tokenRaw, ValidFrom = currentDate, ValidUntil = currentDate.AddMonths(12) };
|
||||||
_securityStore.SetToken(token, user.UserId);
|
_securityStore.SetToken(token, user.UserId);
|
||||||
|
await _identityRepository.UpdateUserAfterAuthentication(user, token);
|
||||||
|
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace IdentityServer.Domain.Data.DbContexts
|
||||||
public class IdentityDbContext : DbContext
|
public class IdentityDbContext : DbContext
|
||||||
{
|
{
|
||||||
public DbSet<AppUser> Users { get; set; }
|
public DbSet<AppUser> Users { get; set; }
|
||||||
|
public DbSet<UserToken> UserTokens { get; set; }
|
||||||
|
|
||||||
public IdentityDbContext(DbContextOptions<IdentityDbContext> options)
|
public IdentityDbContext(DbContextOptions<IdentityDbContext> options)
|
||||||
: base(options)
|
: base(options)
|
||||||
|
@ -22,6 +23,7 @@ namespace IdentityServer.Domain.Data.DbContexts
|
||||||
modelBuilder.ApplyConfiguration(new UserStatusConfiguration());
|
modelBuilder.ApplyConfiguration(new UserStatusConfiguration());
|
||||||
modelBuilder.ApplyConfiguration(new AppUserConfiguration());
|
modelBuilder.ApplyConfiguration(new AppUserConfiguration());
|
||||||
modelBuilder.ApplyConfiguration(new UserClaimConfiguration());
|
modelBuilder.ApplyConfiguration(new UserClaimConfiguration());
|
||||||
|
modelBuilder.ApplyConfiguration(new UserTokenConfiguration());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
using IdentityServer.Domain.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
|
||||||
|
namespace IdentityServer.Domain.Data.EntityTypeConfiguration
|
||||||
|
{
|
||||||
|
class UserTokenConfiguration : IEntityTypeConfiguration<UserToken>
|
||||||
|
{
|
||||||
|
public void Configure(EntityTypeBuilder<UserToken> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("UserToken").HasKey(z => z.Id);
|
||||||
|
builder.Property(z => z.Id).ValueGeneratedOnAdd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
using IdentityServer.Domain.Data.DbContexts;
|
using IdentityServer.Domain.Data.DbContexts;
|
||||||
using IdentityServer.Domain.Entities;
|
using IdentityServer.Domain.Entities;
|
||||||
|
using IdentityServer.Domain.Models;
|
||||||
using IdentityServer.Domain.Repositories;
|
using IdentityServer.Domain.Repositories;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace IdentityServer.Domain.Data.Repositories
|
namespace IdentityServer.Domain.Data.Repositories
|
||||||
|
@ -15,12 +17,29 @@ namespace IdentityServer.Domain.Data.Repositories
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<AppUser> GetAppUser(string userName, string password)
|
public Task<AppUser> GetUser(string userName, string password)
|
||||||
{
|
{
|
||||||
return _dbContext.Users
|
return _dbContext.Users
|
||||||
.Include(z => z.Status)
|
.Include(z => z.Status)
|
||||||
.Include(z => z.Claims)
|
.Include(z => z.Claims)
|
||||||
.FirstOrDefaultAsync(z => z.UserName == userName && z.Password == password);
|
.FirstOrDefaultAsync(z => z.UserName == userName && z.Password == password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,9 +16,9 @@ begin
|
||||||
LastName varchar(100),
|
LastName varchar(100),
|
||||||
Email varchar(100),
|
Email varchar(100),
|
||||||
ProfilePictureUrl varchar(200),
|
ProfilePictureUrl varchar(200),
|
||||||
SecurityStamp varchar(200) constraint UQ_AppUser_SecurityStamp unique,
|
SecurityStamp varchar(200) not null constraint UQ_AppUser_SecurityStamp unique,
|
||||||
StatusId int not null constraint FK_AppUser_UserStatus references UserStatus(StatusId),
|
StatusId int not null constraint FK_AppUser_UserStatus references UserStatus(StatusId),
|
||||||
CreationDate datetime constraint DF_AppUser_CreationDate default getdate(),
|
CreationDate datetime not null constraint DF_AppUser_CreationDate default getdate(),
|
||||||
FailedLoginAttempts int,
|
FailedLoginAttempts int,
|
||||||
LastLoginDate datetime,
|
LastLoginDate datetime,
|
||||||
PasswordChangeDate datetime
|
PasswordChangeDate datetime
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
if not exists (select top 1 1 from sys.objects where name = 'UserToken' and type = 'U')
|
||||||
|
begin
|
||||||
|
create table UserToken
|
||||||
|
(
|
||||||
|
Id int identity(1, 1) constraint PK_Token primary key,
|
||||||
|
UserId int not null constraint FK_Token_AppUser foreign key references AppUser(UserId),
|
||||||
|
Token varchar(1000) not null,
|
||||||
|
ValidFrom datetime not null,
|
||||||
|
ValidUntil datetime not null
|
||||||
|
)
|
||||||
|
end
|
||||||
|
go
|
|
@ -0,0 +1,13 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace IdentityServer.Domain.Entities
|
||||||
|
{
|
||||||
|
public class UserToken
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int UserId { get; set; }
|
||||||
|
public string Token { get; set; }
|
||||||
|
public DateTime ValidFrom { get; set; }
|
||||||
|
public DateTime ValidUntil { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,12 @@
|
||||||
using IdentityServer.Domain.Entities;
|
using IdentityServer.Domain.Entities;
|
||||||
|
using IdentityServer.Domain.Models;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace IdentityServer.Domain.Repositories
|
namespace IdentityServer.Domain.Repositories
|
||||||
{
|
{
|
||||||
public interface IIdentityRepository
|
public interface IIdentityRepository
|
||||||
{
|
{
|
||||||
Task<AppUser> GetAppUser(string userName, string password);
|
Task<AppUser> GetUser(string userName, string password);
|
||||||
|
Task UpdateUserAfterAuthentication(AppUser user, Token token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue