feat: add user management, refs NOISSUE
This commit is contained in:
40
song_of_the_day/Controllers/AuthController.cs
Normal file
40
song_of_the_day/Controllers/AuthController.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
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("/");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user