Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set display name / username for email local accounts in Azure AD B2C?

Tags:

azure-ad-b2c

I'm new to Azure AD B2C and have set up a site that correctly authenticates using local accounts (email only). When the validation request comes back, I see the email address under the 'emails' claim, but the 'name' claim comes back as 'unknown'.

Looking in Azure portal, the account is created but the name is unset and is 'unknown' for all users that register. This isn't what I was expecting. I would prefer that the 'name' be set to the email address by default so that it is easier to find the account in the portal, since we aren't collecting a 'Display Name' at all for this account type (user already enters given and surname).

Do I have something configured incorrectly, and is there a way to default the username to the email address for local, email only accounts?

like image 257
SWC Avatar asked Mar 10 '23 21:03

SWC


1 Answers

Azure AD B2C does not "auto-populate" any fields.

When you setup your sign-up policy or unified sign-up/sign-in policy you get to pick the Sign-up attributes. These are the attributes that are show to the user for him/her to provide and are then stored in Azure AD B2C.

Anything that the user is not prompted for is left empty or in a few select cases (like name as you have observed) set to 'unknown'.

Azure AD B2C can not make assumptions as to what to pre-populate a given attribute with. While you might find it acceptable to use the email as the default for the name, others might not. Another example, the display name, for some, can be prepopulated with "{Given name} {Surname}", but for others, it's the other way around "{Surname, Givenname}".

What you are effectively asking for is an easy way to configure defaults for some attributes which is not that's available today. You can request this feature in the Azure AD B2C UserVoice forum.

At this time, you have two options:

  1. Force your users to explicitly provide this value by select it as a sign-up attribute in your policy.
  2. Add some code that updates these attributes with whatever logic you want (for example in the controller that processes new sign-ups or via a headless client running periodically).

Here's a quick & dirty snippet of .Net code that you can use for this (assuming you want to do this in the auth pipeline (Startup.Auth.cs):

private async Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
{
    try
    {
        var userObjectId = notification.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;

        // You'll need to register a separate app for this.
        // This app will need APPLICATION (not Delegated) Directory.Read permissions
        // Check out this link for more info:
        // https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet 
        var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(string.Format(graphAuthority, tenant));
        var t = await authContext.AcquireTokenAsync(graphResource, new ClientCredential(graphClientId, graphClientSecret));

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + t.AccessToken);

            var url = graphResource + tenant + "/users/" + userObjectId + "/?api-version=1.6";
            var name = "myDisplayName";
            var content = new StringContent("{ \"displayName\":\"" + name + "\" }", Encoding.UTF8, "application/json");
            var result = await client.PostAsync(url, content);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

You'll reference this method when you setup your OpenIdConnectAuthenticationOptions like so:

new OpenIdConnectAuthenticationOptions
    {
        // (...)
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
            AuthenticationFailed = OnAuthenticationFailed,
            SecurityTokenValidated = OnSecurityTokenValidated,
        },
        // (...)
    };
like image 199
Saca Avatar answered Apr 28 '23 10:04

Saca