mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using MyCore.Interfaces.DTO;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Service.Services;
|
|
using MyCore.Services;
|
|
|
|
namespace MyCore.Controllers
|
|
{
|
|
[Authorize]
|
|
[Route("api/token")]
|
|
[ApiController]
|
|
public class TokenController : ControllerBase
|
|
{
|
|
private TokensService _tokenService;
|
|
private UserDatabaseService _userService;
|
|
|
|
public TokenController(TokensService tokenService, UserDatabaseService userService)
|
|
{
|
|
_tokenService = tokenService;
|
|
_userService = userService;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
public ActionResult<UserInfo> ConnectUser([FromBody] LoginDTO loginDTO)
|
|
{
|
|
//string test = _TokenService.GenerateSHA256String(password);
|
|
|
|
if (IsValidUserAndPasswordCombination(loginDTO.Email, loginDTO.Password))
|
|
{
|
|
UserInfo user = _userService.GetByEmail(loginDTO.Email);
|
|
user.Token = _tokenService.GenerateToken(loginDTO.Email).ToString();
|
|
|
|
return user;
|
|
}
|
|
return BadRequest();
|
|
}
|
|
|
|
private bool IsValidUserAndPasswordCombination(string email, string password)
|
|
{
|
|
// Test if is database and is correct
|
|
List<UserInfo> users = _userService.GetAll();
|
|
|
|
UserInfo user = users.Where(u => u.Email == email).FirstOrDefault();
|
|
|
|
if (user != null && user.Password == password)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
|
|
/*if (email == "thomas.fransolet@hotmail.be" && password == "MonsieurMagic") { return true; }
|
|
else return false;*/
|
|
}
|
|
|
|
}
|
|
}
|