Files
rr3-server/RR3CommunityServer/Pages/Sessions.cshtml.cs
Daniel Elliott e03c1d9856 Add admin panel authentication and login system
Features:
- Login page with username/email + password
- Registration page for new accounts
- Logout functionality
- Cookie-based authentication (30-day sessions)
- Auto-redirect to login for unauthorized access
- User dropdown in navbar with logout link

Security:
- All admin pages now require authentication
- [Authorize] attribute on all admin PageModels
- Redirect to /Login if not authenticated
- Auto-login after registration

UI:
- Beautiful gradient login/register pages
- Consistent styling with admin panel
- User info displayed in navbar
- Logout link in dropdown menu

Starting resources for new users:
- 100,000 Gold
- 500,000 Cash
- Level 1
- Full admin panel access

Ready for production deployment!
2026-02-19 15:06:08 -08:00

59 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using RR3CommunityServer.Data;
using static RR3CommunityServer.Data.RR3DbContext;
namespace RR3CommunityServer.Pages;
[Authorize]
public class SessionsModel : PageModel
{
private readonly RR3DbContext _context;
public SessionsModel(RR3DbContext context)
{
_context = context;
}
public List<Session> AllSessions { get; set; } = new();
public List<Session> ActiveSessions { get; set; } = new();
public List<Session> ExpiredSessions { get; set; } = new();
public async Task OnGetAsync()
{
AllSessions = await _context.Sessions
.OrderByDescending(s => s.CreatedAt)
.ToListAsync();
var now = DateTime.UtcNow;
ActiveSessions = AllSessions.Where(s => s.ExpiresAt > now).ToList();
ExpiredSessions = AllSessions.Where(s => s.ExpiresAt <= now).ToList();
}
public async Task<IActionResult> OnPostDeleteAsync(int sessionId)
{
var session = await _context.Sessions.FindAsync(sessionId);
if (session != null)
{
_context.Sessions.Remove(session);
await _context.SaveChangesAsync();
}
return RedirectToPage();
}
public async Task<IActionResult> OnPostCleanupExpiredAsync()
{
var expiredSessions = await _context.Sessions
.Where(s => s.ExpiresAt <= DateTime.UtcNow)
.ToListAsync();
_context.Sessions.RemoveRange(expiredSessions);
await _context.SaveChangesAsync();
return RedirectToPage();
}
}