refactor(auth): Refactoring der Login-Methoden für OpenAPI-Kompatibilität
– Die Login-Methoden wurden überarbeitet, um NotImplementedException auszulösen und OpenAPI (Swagger) und Skalar zu konfigurieren. – Unnötige using-Anweisungen wurden entfernt, um den Code zu optimieren.
This commit is contained in:
parent
2d3987b81e
commit
a85397a363
@ -5,19 +5,18 @@ using Microsoft.AspNetCore.Authentication.Cookies;
|
|||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using DigitalData.UserManager.Application.DTOs.Auth;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using EnvelopeGenerator.GeneratorAPI.Models;
|
using EnvelopeGenerator.GeneratorAPI.Models;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
namespace EnvelopeGenerator.GeneratorAPI.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Controller verantwortlich für die Benutzer-Authentifizierung, einschließlich Anmelden, Abmelden und Überprüfung des Authentifizierungsstatus.
|
||||||
|
/// </summary>
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public partial class AuthController : ControllerBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Controller verantwortlich für die Benutzer-Authentifizierung, einschließlich Anmelden, Abmelden und Überprüfung des Authentifizierungsstatus.
|
|
||||||
/// </summary>
|
|
||||||
[Route("api/[controller]")]
|
|
||||||
[ApiController]
|
|
||||||
public partial class AuthController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly ILogger<AuthController> _logger;
|
private readonly ILogger<AuthController> _logger;
|
||||||
private readonly IUserService _userService;
|
private readonly IUserService _userService;
|
||||||
private readonly IDirectorySearchService _dirSearchService;
|
private readonly IDirectorySearchService _dirSearchService;
|
||||||
@ -65,58 +64,10 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
|||||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> Login([FromBody] Login login, [FromQuery] bool cookie = false)
|
public Task<IActionResult> Login([FromBody] Login login, [FromQuery] bool cookie = false)
|
||||||
{
|
{
|
||||||
try
|
// added to configure open API (swagger and scalar)
|
||||||
{
|
throw new NotImplementedException();
|
||||||
bool isValid = _dirSearchService.ValidateCredentials(login.Username, login.Password);
|
|
||||||
|
|
||||||
if (!isValid)
|
|
||||||
return Unauthorized();
|
|
||||||
|
|
||||||
//find the user
|
|
||||||
var uRes = await _userService.ReadByUsernameAsync(login.Username);
|
|
||||||
if (!uRes.IsSuccess || uRes.Data is null)
|
|
||||||
{
|
|
||||||
return Forbid();
|
|
||||||
}
|
|
||||||
|
|
||||||
UserReadDto user = uRes.Data;
|
|
||||||
|
|
||||||
// Create claims
|
|
||||||
var claims = new List<Claim>
|
|
||||||
{
|
|
||||||
new (ClaimTypes.NameIdentifier, user.Id.ToString()),
|
|
||||||
new (ClaimTypes.Name, user.Username),
|
|
||||||
new (ClaimTypes.Surname, user.Name!),
|
|
||||||
new (ClaimTypes.GivenName, user.Prename!),
|
|
||||||
new (ClaimTypes.Email, user.Email!),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create claimsIdentity
|
|
||||||
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
||||||
|
|
||||||
// Create authProperties
|
|
||||||
var authProperties = new AuthenticationProperties
|
|
||||||
{
|
|
||||||
IsPersistent = true,
|
|
||||||
AllowRefresh = true,
|
|
||||||
ExpiresUtc = DateTime.Now.AddMinutes(180)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Sign in
|
|
||||||
await HttpContext.SignInAsync(
|
|
||||||
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
||||||
new ClaimsPrincipal(claimsIdentity),
|
|
||||||
authProperties);
|
|
||||||
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "Unexpected error occurred.\n{ErrorMessage}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -143,9 +94,10 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
|||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Route("form")]
|
[Route("form")]
|
||||||
public async Task<IActionResult> Login([FromForm] Login login)
|
public Task<IActionResult> Login([FromForm] Login login)
|
||||||
{
|
{
|
||||||
return await Login(login, true);
|
// added to configure open API (swagger and scalar)
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -197,5 +149,4 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult IsAuthenticated() => Ok();
|
public IActionResult IsAuthenticated() => Ok();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user