40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
|
|
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<IActionResult> Login(string username, string password)
|
|
{
|
|
var ldapService = HttpContext.RequestServices.GetService<LdapAuthenticationService>();
|
|
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<IActionResult> Logout()
|
|
{
|
|
await HttpContext.SignOutAsync();
|
|
return RedirectToSamePageIfPossible();
|
|
}
|
|
|
|
private IActionResult RedirectToSamePageIfPossible()
|
|
{
|
|
if (Request.Headers.ContainsKey("Referer"))
|
|
{
|
|
return Redirect(Request.Headers["Referer"].ToString());
|
|
}
|
|
return RedirectToPage("/");
|
|
}
|
|
} |