summaryrefslogtreecommitdiff
path: root/Controllers/LoginController.cs
blob: fff3e6eca7cfa2b083aece51772c897f01f7dce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace HyperBooru.Controllers;

[ApiController]
[Route("/")]
public class LoginController : Controller {
    private IHttpContextAccessor httpContextAccessor;
    public LoginController(IHttpContextAccessor httpContextAccessor) =>
        this.httpContextAccessor = httpContextAccessor;

    [HttpPost("Login")]
    public async Task<IActionResult> Login(
        [FromForm] string username,
        [FromForm] string password) {

        var claims = new Claim[] {
            new Claim(ClaimTypes.NameIdentifier, username)
        };

        var claimsIdentity = new ClaimsIdentity(
            claims,
            CookieAuthenticationDefaults.AuthenticationScheme);

        var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

        if(username == "admin" && password == "test") {
            await httpContextAccessor.HttpContext!.SignInAsync(claimsPrincipal);
            return Ok();
        } else {
            return StatusCode(403);
        }
    }

    [HttpPost("Logout")]
    public async Task Logout() =>
        await httpContextAccessor.HttpContext!.SignOutAsync();
}