Skip to content

For applications leveraging Azure B2C which want a clean way to read the claims, you have passed in your workflow back to the application.

License

Notifications You must be signed in to change notification settings

jonathansear/Get-claims-from-Azure-B2C-jwt-token

Repository files navigation

Convert the ClaimsPrincipal object returned by Azure B2C into this adapter to read the user claims

Introduction

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.

Usage

ClaimProperties userClaims = _tokenManager.ConvertClaims(User.Identities.FirstOrDefault().Claims);

string name = userClaims.name;

string jobTitle = userClaims.jobTitle;

Setup

  1. 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));
  2. Add a project reference to the Managers class libary from within this repo.

  3. 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()));
      }
  1. 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();
        }
   

About

For applications leveraging Azure B2C which want a clean way to read the claims, you have passed in your workflow back to the application.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published