using System.Collections.Generic; using System.Linq; using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using BlazorShared.Authorization; namespace Microsoft.eShopWeb.Web.Controllers { [Route("[controller]")] [ApiController] public class UserController : ControllerBase { [HttpGet] [Authorize] [AllowAnonymous] public IActionResult GetCurrentUser() => Ok(User.Identity.IsAuthenticated ? CreateUserInfo(User) : UserInfo.Anonymous); private UserInfo CreateUserInfo(ClaimsPrincipal claimsPrincipal) { if (!claimsPrincipal.Identity.IsAuthenticated) { return UserInfo.Anonymous; } var userInfo = new UserInfo { IsAuthenticated = true }; if (claimsPrincipal.Identity is ClaimsIdentity claimsIdentity) { userInfo.NameClaimType = claimsIdentity.NameClaimType; userInfo.RoleClaimType = claimsIdentity.RoleClaimType; } else { userInfo.NameClaimType = "name"; userInfo.RoleClaimType = "role"; } if (claimsPrincipal.Claims.Any()) { var claims = new List(); var nameClaims = claimsPrincipal.FindAll(userInfo.NameClaimType); foreach (var claim in nameClaims) { claims.Add(new ClaimValue(userInfo.NameClaimType, claim.Value)); } foreach (var claim in claimsPrincipal.Claims.Except(nameClaims)) { claims.Add(new ClaimValue(claim.Type, claim.Value)); } userInfo.Claims = claims; } return userInfo; } } }