42 lines
1,001 B
C#
42 lines
1,001 B
C#
using Authinator.Backend.Database;
|
|
using Authinator.Backend.Database.Tables;
|
|
using Authinator.Backend.Utils;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
namespace Authinator.Pages;
|
|
|
|
public class LoginModel : PageModel {
|
|
public IActionResult OnGet() {
|
|
var db = new DatabaseContext();
|
|
|
|
if (HttpContext.IsAuthenticated(db))
|
|
return Redirect("/User");
|
|
|
|
return Page();
|
|
}
|
|
|
|
public IActionResult OnPost() {
|
|
var db = new DatabaseContext();
|
|
|
|
if (HttpContext.IsAuthenticated(db))
|
|
return Redirect("/User");
|
|
|
|
if (Request.Form["action"] == "login") {
|
|
var user = db.Users.FirstOrDefault(p => p.Username == Request.Form["username"].ToString());
|
|
if (user == null) {
|
|
return Redirect("/Login");
|
|
}
|
|
|
|
if (!user.ValidatePassword(Request.Form["password"].ToString()))
|
|
return Redirect("/Login");
|
|
|
|
Response.AppendUnencodedCookie(ConfigCache.CookieName, user.GetAuthToken());
|
|
|
|
return Redirect("/User");
|
|
}
|
|
|
|
return Redirect("/Login");
|
|
}
|
|
}
|