dnn.azureadb2cprovider icon indicating copy to clipboard operation
dnn.azureadb2cprovider copied to clipboard

AzureB2C module not AutoMatchExistingUsers

Open allschu opened this issue 2 years ago • 0 comments

I already have existing users inside my DNN portal. They have a username like "john" and an emailaddress like "[email protected]"

The idea is that users are going to start login using Azure B2C instead of the normal DNN Login page. Zo users try to login via Azure B2C and after a successfull login they are redirected back to DNN. The AzureClient.cs has the following code.

 var usernamePrefixEnabled = bool.Parse(AzureConfig.GetSetting(AzureConfig.ServiceName, "UsernamePrefixEnabled", portalSettings.PortalId, "true"));
            var usernameToFind = usernamePrefixEnabled ? $"{AzureConfig.ServiceName}-{userClaim.Value}" : userClaim.Value;
            var userInfo = UserController.GetUserByName(portalSettings.PortalId, usernameToFind);
            // If user doesn't exist on current portal, AuthenticateUser() will create it. 
            // Otherwise, AuthenticateUser will perform a Response.Redirect, so we have to sinchronize the roles before that, to avoid the ThreadAbortException caused by the Response.Redirect
            if (userInfo == null)
            {
                base.AuthenticateUser(user, portalSettings, IPAddress, addCustomProperties, onAuthenticated);
                if (IsCurrentUserAuthorized())
                {
                    userInfo = UserController.GetUserByName(portalSettings.PortalId, usernameToFind);
                    if (userInfo == null)
                    {
                        throw new SecurityTokenException($"The logged in user {usernameToFind} does not belong to PortalId {portalSettings.PortalId}");
                    }
                    UpdateUserAndRoles(userInfo);
                    MarkUserAsB2c(userInfo);
                }
            }
            else
            {
                if (IsCurrentUserAuthorized())
                {
                    UpdateUserAndRoles(userInfo);
                    MarkUserAsB2c(userInfo);
                }
                base.AuthenticateUser(user, portalSettings, IPAddress, addCustomProperties, onAuthenticated);
            }

After the login the var userInfo = UserController.GetUserByName(portalSettings.PortalId, usernameToFind);

has successfully found the user in the database. So the userInfo object is not null. So far so good. And the end the code enters base.AuthenticateUser and DNN tries to create a new user, instead of using the already existing user.

Why is this happening?? The AutoMatchExistingUsers is true.

allschu avatar Jul 15 '22 11:07 allschu