Task-13
Task-13
builder.Services.AddControllersWithViews();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
app.UseSession();
app.UseRouting();
app.Run();
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Login</h2>
This controller will handle login logic and store the session.
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Threading.Tasks;
[HttpPost]
public async Task<IActionResult> Login(string Username, string Password)
{
// Dummy authentication logic (replace with actual database validation)
if (Username == "admin" && Password == "password")
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Username)
};
<h2>Dashboard</h2>
@if (!string.IsNullOrEmpty(username))
{
<p>Welcome, @username!</p>
<a asp-controller="Account" asp-action="Logout">Logout</a>
}
else
{
<p>Please <a asp-controller="Account" asp-action="Login">login</a>.</p>
}
6: Apply [Authorize] to Secure the Dashboard
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Final Output: