Files
eShopOnWeb/src/Web/Controllers/UserController.cs
Philippe Vaillancourt a72dd775ee Refactor to introduce nullable types and reduce compiler warnings (#801)
* Introduce nullable types to Core, Infrastructure and PublicApi projects

* Refactor unit tests

* Fixed failing tests

* Introduce Parameter object for CatalogItem update method

* Refactor CataloItemDetails param object

* Refactor following PR comments
2022-10-04 11:57:40 -04:00

75 lines
2.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using BlazorShared.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
namespace Microsoft.eShopWeb.Web.Controllers;
[Route("[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly ITokenClaimsService _tokenClaimsService;
public UserController(ITokenClaimsService tokenClaimsService)
{
_tokenClaimsService = tokenClaimsService;
}
[HttpGet]
[Authorize]
[AllowAnonymous]
public async Task<IActionResult> GetCurrentUser() =>
Ok(await CreateUserInfo(User));
private async Task<UserInfo> CreateUserInfo(ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal.Identity == null || claimsPrincipal.Identity.Name == null || !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<ClaimValue>();
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;
}
var token = await _tokenClaimsService.GetTokenAsync(claimsPrincipal.Identity.Name);
userInfo.Token = token;
return userInfo;
}
}