47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
|
using IdentityServer.Application.Stores;
|
|||
|
using IdentityServer.Domain.Entities;
|
|||
|
using IdentityServer.Domain.Models;
|
|||
|
using IdentityServer.Domain.Repositories;
|
|||
|
using System;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace IdentityServer.Application.Services
|
|||
|
{
|
|||
|
public class UserService : IUserService
|
|||
|
{
|
|||
|
private readonly ISecurityStore _securityStore;
|
|||
|
private readonly IIdentityRepository _identityRepository;
|
|||
|
|
|||
|
public UserService(ISecurityStore securityStore, IIdentityRepository identityRepository)
|
|||
|
{
|
|||
|
_securityStore = securityStore;
|
|||
|
_identityRepository = identityRepository;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<Token> Authenticate(string userName, string password)
|
|||
|
{
|
|||
|
var user = await _identityRepository.GetAppUser(userName, password);
|
|||
|
if (user == null)
|
|||
|
return null;
|
|||
|
|
|||
|
var tokenRaw = $"{Guid.NewGuid()}-{Guid.NewGuid()}-{user.UserId}";
|
|||
|
_securityStore.SetToken(tokenRaw, user.UserId);
|
|||
|
|
|||
|
var token = new Token() { Raw = tokenRaw };
|
|||
|
return token;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<AppUser> Authorize(string token)
|
|||
|
{
|
|||
|
var tokenValidation = _securityStore.ValidateToken(token);
|
|||
|
if (tokenValidation.Success)
|
|||
|
{
|
|||
|
var user = await _identityRepository.GetAppUser(tokenValidation.UserId);
|
|||
|
return user;
|
|||
|
}
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|