You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.9 KiB
63 lines
1.9 KiB
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<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;
|
|
}
|
|
|
|
return userInfo;
|
|
}
|
|
}
|
|
}
|