SirCypkowskyy
SirCypkowskyy
CC#
Created by SirCypkowskyy on 6/8/2024 in #help
Bearer token is not used by Swagger for endpoints with [authorize], but used for [AllowAnonymous]
No description
9 replies
CC#
Created by SirCypkowskyy on 6/8/2024 in #help
Bearer token is not used by Swagger for endpoints with [authorize], but used for [AllowAnonymous]
endpoint in UserController.cs
/// <summary>
/// Zwraca informacje o zalogowanym użytkowniku
/// </summary>
/// <returns>
/// Informacje o zalogowanym użytkowniku
/// </returns>
[HttpGet("auth/self")]
// [Authorize(AuthenticationSchemes = "Bearer")]
[AllowAnonymous]
public async Task<IActionResult> GetSelfInfoAsync()
{
var user = User;
var claims = user.Claims;

_logger.LogInformation("User {Username} requested self info", user.FindFirstValue(ClaimTypes.Name));
_logger.LogDebug("User {Username} requested self info with claims: {Claims}", user.FindFirstValue(ClaimTypes.Name), claims);

var userId = user.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new UnauthorizedAccessException("User not found");
var userIdAsGuid = Guid.Parse(userId);

return Ok(new UserJwtInfoResponseDTO()
{
Username = user.FindFirstValue(ClaimTypes.Name),
Email = user.FindFirstValue(ClaimTypes.Email),
Role = user.FindFirstValue(ClaimTypes.Role)
});
}
/// <summary>
/// Zwraca informacje o zalogowanym użytkowniku
/// </summary>
/// <returns>
/// Informacje o zalogowanym użytkowniku
/// </returns>
[HttpGet("auth/self")]
// [Authorize(AuthenticationSchemes = "Bearer")]
[AllowAnonymous]
public async Task<IActionResult> GetSelfInfoAsync()
{
var user = User;
var claims = user.Claims;

_logger.LogInformation("User {Username} requested self info", user.FindFirstValue(ClaimTypes.Name));
_logger.LogDebug("User {Username} requested self info with claims: {Claims}", user.FindFirstValue(ClaimTypes.Name), claims);

var userId = user.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new UnauthorizedAccessException("User not found");
var userIdAsGuid = Guid.Parse(userId);

return Ok(new UserJwtInfoResponseDTO()
{
Username = user.FindFirstValue(ClaimTypes.Name),
Email = user.FindFirstValue(ClaimTypes.Email),
Role = user.FindFirstValue(ClaimTypes.Role)
});
}
9 replies
CC#
Created by SirCypkowskyy on 6/8/2024 in #help
Bearer token is not used by Swagger for endpoints with [authorize], but used for [AllowAnonymous]
my Program.cs
9 replies