tycg_carviolation_BE/Traffic.Service/Helpers/ParserForUserFromClaim.cs

46 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace LiangLiSystem.Services.Helpers
{
public class ParserForUserFromClaim
{
public ClaimsIdentity userIdentity { get; set; }
public ParserForUserFromClaim(ClaimsIdentity c)
{
userIdentity = c;
}
public int GetUserId()
{
if (userIdentity == null) { return 0; }
var result = userIdentity.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier);
return Convert.ToInt16(result.FirstOrDefault().Value);
}
public string GetUserName()
{
if (userIdentity == null) { return string.Empty; }
var result = userIdentity.Claims.Where(c => c.Type == ClaimTypes.GivenName);
return result.FirstOrDefault().Value.ToString();
}
public string GetAccount()
{
if (userIdentity == null) { return string.Empty; }
var result = userIdentity.Claims.Where(c => c.Type == "name");
return result.FirstOrDefault().Value.ToString();
}
}
}