using System; using System.Linq; using System.Threading.Tasks; using API.Extensions; using API.Services; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace API.Controllers; [Route("[controller]")] public class OidcController: ControllerBase { [AllowAnonymous] [HttpGet("login")] public IActionResult Login(string returnUrl = "/") { var properties = new AuthenticationProperties { RedirectUri = returnUrl }; return Challenge(properties, IdentityServiceExtensions.OpenIdConnect); } [HttpGet("logout")] public async Task Logout() { if (!Request.Cookies.ContainsKey(OidcService.CookieName)) { return Redirect("/"); } var res = await Request.HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme); if (!res.Succeeded || res.Properties == null || string.IsNullOrEmpty(res.Properties.GetString(OidcService.IdToken))) { HttpContext.Response.Cookies.Delete(OidcService.CookieName); return Redirect("/"); } return SignOut( new AuthenticationProperties { RedirectUri = "/login" }, CookieAuthenticationDefaults.AuthenticationScheme, IdentityServiceExtensions.OpenIdConnect); } }