Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Yashna 2 posts 72 karma points
    Jul 16, 2024 @ 06:39
    Yashna
    0

    Retrieving data from Microsoft Entra ID and updating Umbraco Member data

    Hello,

    I am trying to update custom member data after a user logs in using an external login, specifically via Microsoft AD. I have successfully updated the basic login details, such as name, member group, login name, and email. However, I need to retrieve a set of custom information from Microsoft Entra ID. Unfortunately, I am unable to retrieve any data or access the custom fields of the Umbraco member.

    I have tried multiple code samples from ChatGPT, but there are packages that do not seem to be compatible with Umbraco 13.

    Has anyone attempted this and succeeded? If so, please share your experience.

    Set of custom data: enter image description here

    Below is the code base I am working with:

    public class EntraIDB2CMembersExternalLoginProviderOptions : IConfigureNamedOptions<MemberExternalLoginProviderOptions>
        {
            public const string SchemeName = "ActiveDirectoryB2C";
    
            public void Configure(string? name, MemberExternalLoginProviderOptions options)
        {
            if (name != Constants.Security.MemberExternalAuthenticationTypePrefix + SchemeName)
            {
                return;
            }
            Configure(options);
        }
    
        public void Configure(MemberExternalLoginProviderOptions options)
        {
            // The following options are relevant if you
            // want to configure auto-linking on the authentication.
            options.AutoLinkOptions = new MemberExternalSignInAutoLinkOptions(
    
                // Set to true to enable auto-linking
                autoLinkExternalAccount: true,
    
                // [OPTIONAL]
                // Default: The culture specified in appsettings.json.
                // Specify the default culture to create the Member as.
                // It can be dynamically assigned in the OnAutoLinking callback.
                defaultCulture: null,
    
                // [OPTIONAL]
                // Specify the default "IsApproved" status.
                // Must be true for auto-linking.
                defaultIsApproved: true,
    
                // [OPTIONAL]
                // Default: "Member"
                // Specify the Member Type alias.
                defaultMemberTypeAlias: Constants.Security.DefaultMemberTypeAlias
            )
            {
                // [OPTIONAL] Callback
                OnAutoLinking = (autoLinkUser, loginInfo) =>
                {
                    // Customize the user before it's linked.
                    // Modify the User's groups based on the Claims returned
                    // in the external login info.
    
                    autoLinkUser.AddRole("GroupA");                
                    autoLinkUser.Name = loginInfo.Principal.FindFirstValue(ClaimTypes.Name);               
    
                    // Customize the user before it's linked.
                    // Extract the necessary claims from loginInfo
                    //var claims = loginInfo.Principal.Claims.ToList();
                    //var email = claims.FirstOrDefault(p => p.Type.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"))?.Value.Replace("#EXT#", string.Empty);
                    var email = autoLinkUser.Email;
                    var firstName = loginInfo.Principal.FindFirstValue(ClaimTypes.GivenName);
                    var lastName = loginInfo.Principal.FindFirstValue(ClaimTypes.Surname);                
    
                },
    
                // [OPTIONAL] Callback
                OnExternalLogin = (user, loginInfo) =>
                {
                    // Customize the User before it is saved whenever they have
                    // logged in with the external provider.
                    // Sync the Users name based on the Claims returned
                    // in the external login info
    
                    // Returns a boolean indicating if sign-in should continue or not.
                    return true;
                }
            };
        }
    }
    

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft