using Microsoft.AspNetCore.Mvc; using System.Security.Claims; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication; public class AuthController : Controller { [HttpPost] public async Task Login(string username, string password) { var ldapService = HttpContext.RequestServices.GetService(); if (ldapService.Authenticate(username, password)) { var claims = new[] { new Claim(ClaimTypes.Name, username) }; var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync(new ClaimsPrincipal(identity)); return RedirectToSamePageIfPossible(); } ViewBag.Error = "Invalid credentials"; return RedirectToSamePageIfPossible(); } [HttpPost] public async Task Logout() { await HttpContext.SignOutAsync(); return RedirectToSamePageIfPossible(); } private IActionResult RedirectToSamePageIfPossible() { if (Request.Headers.ContainsKey("Referer")) { return Redirect(Request.Headers["Referer"].ToString()); } return RedirectToPage("/"); } }