For applications leveraging Azure B2C which want a clean way to read the claims you have passed in your workflow back to the application.
This small lightweight library unpicks all the claims in the ClaimsPrincipal.
ClaimProperties userClaims = _tokenManager.ConvertClaims(User.Identities.FirstOrDefault().Claims);
string name = userClaims.name;
string jobTitle = userClaims.jobTitle;
-
Your application must already be using Azure B2C. You should see something similar startup.cs.
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme) .AddAzureADB2C(options => Configuration.Bind(CurrentEnvironment.IsDevelopment() ? "AzureAdB2CDev" : "AzureAdB2C", options));
-
Add a project reference to the Managers class libary from within this repo.
-
Register the dependency in your startup.cs.
public void ConfigureServices(IServiceCollection services)
{
//Add Token Manager to dependency injection
services.Add(new ServiceDescriptor(typeof(ITokenManager), new TokenManager()));
}
- Finally Inject it into your controller
ITokenManager _tokenManager;
public HomeController(ITokenManager tokenManager)
{
_tokenManager = tokenManager;
}
public IActionResult Index()
{
ClaimProperties userClaims = _tokenManager.ConvertClaims(User.Identities.FirstOrDefault().Claims);
string name = userClaims.name;
string jobTitle = userClaims.jobTitle;
return View();
}