This repository has been archived on 2023-04-02. You can view files and clone it, but cannot push or open issues or pull requests.
trainav/trainav.web/Pages/Register.cshtml.cs

31 lines
1.0 KiB
C#

using System.Linq;
using LinqToDB;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;
using trainav.web.database;
using trainav.web.database.Tables;
namespace trainav.web.Pages {
public class RegisterModel : PageModel {
public void OnPost() {
if (!Request.HasFormContentType
|| string.IsNullOrWhiteSpace(Request.Form["user"])
|| string.IsNullOrWhiteSpace(Request.Form["pass"])
|| string.IsNullOrWhiteSpace(Request.Form["code"]))
return;
if (Request.Form["code"] != System.IO.File.ReadAllLines("regkey.txt")[0])
return;
using var db = new Database.DbConn();
var user = db.Users.FirstOrDefault(p => p.Username == Request.Form["user"].ToString());
if (user != null)
return; //user already exists
var uid = db.InsertWithInt32Identity(new User {Username = Request.Form["user"].ToString(), Password = Request.Form["pass"].ToString().Sha256()});
HttpContext.Session.SetString("uid", uid.ToString());
HttpContext.Session.SetString("authorized", "true");
}
}
}