Add Account page
This commit is contained in:
53
Pages/Account.cshtml
Normal file
53
Pages/Account.cshtml
Normal file
@ -0,0 +1,53 @@
|
||||
@page
|
||||
@model NightmareCoreWeb2.Pages.AccountModel
|
||||
@{
|
||||
}
|
||||
<div class="container">
|
||||
@if (string.IsNullOrEmpty(Model.AuthToken)) {
|
||||
<div id="LoginForm">
|
||||
<form action="?handler=Login" method="post" enctype="multipart/form-data">
|
||||
<div class="form-group">
|
||||
<label for="UserEmail">E-mail:</label>
|
||||
<input asp-for="UserEmail" type="text" id="UserEmail" />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="UserPassword">Password:</label>
|
||||
<input asp-for="UserPassword" type="password" id="UserPassword">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
} else {
|
||||
<!-- Page Content -->
|
||||
<div id="page-content-wrapper">
|
||||
|
||||
|
||||
<div class="container-fluid">
|
||||
<h1 class="mt-4">@Model.CharacterListType</h1>
|
||||
<table class="table table-striped table-dark" >
|
||||
<thead class="thead-dark">
|
||||
<tr>
|
||||
<th scope="col">Player</th>
|
||||
<th scope="col">Character</th>
|
||||
<th scope="col">Level</th>
|
||||
<th scope="col">Race</th>
|
||||
<th scope="col">Class</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var character in Model.OnlineCharacters) {
|
||||
<tr>
|
||||
<td>@character.Username</td>
|
||||
<td>@character.Name</td>
|
||||
<td>@character.Level</td>
|
||||
<td>@character.GetRace()</td>
|
||||
<td>@character.GetClass()</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<!-- /#page-content-wrapper -->
|
||||
}
|
||||
|
||||
</div>
|
||||
63
Pages/Account.cshtml.cs
Normal file
63
Pages/Account.cshtml.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace NightmareCoreWeb2.Pages
|
||||
{
|
||||
public class AccountModel : PageModel
|
||||
{
|
||||
string connStr = $"SslMode=None;server={Program.MysqlServer};user={Program.MysqlUser};database={Program.MysqlDatabase};port={Program.MysqlPort};password={Program.MysqlPassword}";
|
||||
public string UserEmail { get; set; }
|
||||
public string UserPassword { get; set; }
|
||||
public string CharacterListType {get; set;}
|
||||
public string AuthToken { get; set; }
|
||||
public string Username {get; set;}
|
||||
|
||||
public List<Character> OnlineCharacters = new List<Character>();
|
||||
|
||||
private readonly ILogger<AccountModel> _logger;
|
||||
|
||||
private MySqlConnection conn;
|
||||
public AccountModel(ILogger<AccountModel> logger)
|
||||
{
|
||||
|
||||
conn = new MySqlConnection(connStr);
|
||||
_logger = logger;
|
||||
}
|
||||
public void OnGet()
|
||||
{
|
||||
|
||||
ViewData["Title"] = "Login";
|
||||
AuthToken = Request.Cookies["AuthToken"];
|
||||
Username = Request.Cookies["Username"];
|
||||
if (!string.IsNullOrEmpty(Username)) {
|
||||
Account a = new Account(Username, conn);
|
||||
OnlineCharacters = a.characters;
|
||||
|
||||
ViewData["Title"] = a.Username;
|
||||
CharacterListType = $"{a.Username}'s Characters";
|
||||
}
|
||||
}
|
||||
public void OnPostLogin()
|
||||
{
|
||||
UserEmail = Request.Form["UserEmail"];
|
||||
UserPassword = Request.Form["UserPassword"];
|
||||
Username = UserEmail.Substring(0, UserEmail.IndexOf("@"));
|
||||
AuthToken = Hash($"{Username.ToUpper()}:{UserPassword.ToUpper()}");
|
||||
|
||||
Response.Cookies.Append("Username", Username);
|
||||
Response.Cookies.Append("AuthToken", AuthToken);
|
||||
}
|
||||
|
||||
static string Hash(string input)
|
||||
{
|
||||
var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
return string.Concat(hash.Select(b => b.ToString("x2")));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user