Files
administration/Controllers/HomeController.cs

47 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using administration.Models;
namespace administration.Controllers;
public class HomeController : Controller
{
private readonly FinancesContext _context;
public HomeController(FinancesContext context)
{
_context = context;
}
public IActionResult Index()
{
// Récupère un utilisateur fictif pour lexemple
var user = _context.Users.Where(X => X.Id == 2).First(); // à remplacer par un filtre réel (par ex. Email ou Id)
if (user != null)
{
// Stocker dans la session
HttpContext.Session.SetInt32("UserId", user.Id);
HttpContext.Session.SetString("UserName", user.Name);
}
return View();
}
public IActionResult Profile()
{
int? userId = HttpContext.Session.GetInt32("UserId");
string userName = HttpContext.Session.GetString("UserName");
ViewBag.UserId = userId;
ViewBag.UserName = userName;
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}