I have an asp.net mvc app, and am utilizing the code from the following sample: https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi
After the user is logged in, I would like to add a system admin role claim to the principal claims, and the best place I can figure is in the Startup.Auth.cs :
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification)
{
try
{
var userEmail = notification.AuthenticationTicket.Identity.
Claims.SingleOrDefault(x => x.Type == ClaimTypes.Email)?.Value;
if (userEmail != null)
{
using (var ctx = new DbContext())
{
var currentUser = ctx.Users.SingleOrDefault(u => u.Email == userEmail);
if (currentUser != null && currentUser.IsAdmin)
{
notification.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimTypes.Role, Common.Configuration.RoleAdministratorName));
}
}
}
This seems to work, but somehow doesn't feel right to instantiate a new db context just for this in startup.auth. Is this normal practice to do it here?