private IdentityUserIndex CreateLoginIndex(string userid, string loginProvider, string providerKey)
 {
     return(new IdentityUserIndex()
     {
         Id = userid,
         PartitionKey = _keyHelper.GeneratePartitionKeyIndexByLogin(loginProvider, providerKey),
         RowKey = _keyHelper.GenerateRowKeyIdentityUserLogin(loginProvider, providerKey),
         KeyVersion = _keyHelper.KeyVersion,
         ETag = Constants.ETagWildcard
     });
 }
        private (List <DynamicTableEntity> targetUserEntities, List <ITableEntity> targetUserIndexes) ConvertToTargetUserEntities(string userId, List <DynamicTableEntity> sourceUserEntities)
        {
            List <DynamicTableEntity> targetUserEntities = new List <DynamicTableEntity>(100);
            List <ITableEntity>       targetUserIndexes  = new List <ITableEntity>(100);

            foreach (DynamicTableEntity sourceEntity in sourceUserEntities)
            {
                if (sourceEntity.PartitionKey.StartsWith(Constants.RowKeyConstants.PreFixIdentityUserId))
                {
                    string targetUserPartitionKey = _keyHelper.GenerateRowKeyUserId(userId);

                    //User record
                    if (sourceEntity.RowKey.StartsWith(Constants.RowKeyConstants.PreFixIdentityUserId))
                    {
                        //New User
                        //Add UserName Index
                        //Add Email Index
                        DynamicTableEntity tgtDte = new DynamicTableEntity(targetUserPartitionKey, targetUserPartitionKey, Constants.ETagWildcard, sourceEntity.Properties);
                        tgtDte.Properties["Id"]         = new EntityProperty(userId);
                        tgtDte.Properties["KeyVersion"] = new EntityProperty(_keyHelper.KeyVersion);
                        targetUserEntities.Add(tgtDte);

                        //UserName index
                        tgtDte.Properties.TryGetValue("UserName", out EntityProperty userNameProperty);
                        string            userNameKey   = _keyHelper.GenerateRowKeyUserName(userNameProperty.StringValue);
                        IdentityUserIndex userNameIndex = new IdentityUserIndex()
                        {
                            Id           = targetUserPartitionKey,
                            PartitionKey = userNameKey,
                            RowKey       = targetUserPartitionKey,
                            KeyVersion   = _keyHelper.KeyVersion,
                            ETag         = Constants.ETagWildcard
                        };
                        targetUserIndexes.Add(userNameIndex);

                        //Email index - only if email exists
                        if (tgtDte.Properties.TryGetValue("Email", out EntityProperty emailProperty))
                        {
                            string            emailKey   = _keyHelper.GenerateRowKeyUserEmail(emailProperty.StringValue);
                            IdentityUserIndex emailIndex = new IdentityUserIndex()
                            {
                                Id           = targetUserPartitionKey,
                                PartitionKey = emailKey,
                                RowKey       = targetUserPartitionKey,
                                KeyVersion   = _keyHelper.KeyVersion,
                                ETag         = Constants.ETagWildcard
                            };
                            targetUserIndexes.Add(emailIndex);
                        }
                        continue;
                    }
                    //User Claim record
                    if (sourceEntity.RowKey.StartsWith(Constants.RowKeyConstants.PreFixIdentityUserClaim))
                    {
                        //New User Claim
                        //Add Claim Index
                        sourceEntity.Properties.TryGetValue("ClaimType", out EntityProperty claimTypeProperty);
                        string claimType = claimTypeProperty.StringValue;
                        sourceEntity.Properties.TryGetValue("ClaimValue", out EntityProperty claimValueProperty);
                        string claimValue = claimValueProperty.StringValue;

                        string             targetUserRowKey = _keyHelper.GenerateRowKeyIdentityUserClaim(claimType, claimValue);
                        DynamicTableEntity tgtDte           = new DynamicTableEntity(targetUserPartitionKey, targetUserRowKey, Constants.ETagWildcard, sourceEntity.Properties);
                        tgtDte.Properties["UserId"]     = new EntityProperty(userId);
                        tgtDte.Properties["KeyVersion"] = new EntityProperty(_keyHelper.KeyVersion);
                        targetUserEntities.Add(tgtDte);

                        //Claim index
                        IdentityUserIndex claimIndex = new IdentityUserIndex()
                        {
                            Id           = targetUserPartitionKey,
                            PartitionKey = targetUserRowKey,
                            RowKey       = targetUserPartitionKey,
                            KeyVersion   = _keyHelper.KeyVersion,
                            ETag         = Constants.ETagWildcard
                        };
                        targetUserIndexes.Add(claimIndex);
                        continue;
                    }
                    //User Logon record
                    if (sourceEntity.RowKey.StartsWith(Constants.RowKeyConstants.PreFixIdentityUserLogin))
                    {
                        //New User Logon
                        //Add Logon Index
                        sourceEntity.Properties.TryGetValue("LoginProvider", out EntityProperty loginProviderProperty);
                        string loginProvider = loginProviderProperty.StringValue;
                        sourceEntity.Properties.TryGetValue("ProviderKey", out EntityProperty providerKeyProperty);
                        string providerKey = providerKeyProperty.StringValue;

                        string             targetUserRowKey = _keyHelper.GenerateRowKeyIdentityUserLogin(loginProvider, providerKey);
                        DynamicTableEntity tgtDte           = new DynamicTableEntity(targetUserPartitionKey, targetUserRowKey, Constants.ETagWildcard, sourceEntity.Properties);
                        tgtDte.Properties["UserId"]     = new EntityProperty(userId);
                        tgtDte.Properties["KeyVersion"] = new EntityProperty(_keyHelper.KeyVersion);
                        targetUserEntities.Add(tgtDte);

                        //Logon index
                        IdentityUserIndex logonIndex = new IdentityUserIndex()
                        {
                            Id           = targetUserPartitionKey,
                            PartitionKey = _keyHelper.GeneratePartitionKeyIndexByLogin(loginProvider, providerKey),
                            RowKey       = _keyHelper.GenerateRowKeyIdentityUserLogin(loginProvider, providerKey),
                            KeyVersion   = _keyHelper.KeyVersion,
                            ETag         = Constants.ETagWildcard
                        };
                        targetUserIndexes.Add(logonIndex);
                        continue;
                    }
                    //User Role record
                    if (sourceEntity.RowKey.StartsWith(Constants.RowKeyConstants.PreFixIdentityUserRole))
                    {
                        //New User Role
                        //Add Role Index
                        sourceEntity.Properties.TryGetValue("RoleName", out EntityProperty roleNameProperty);
                        string roleName = roleNameProperty.StringValue;

                        string             targetUserRowKey = _keyHelper.GenerateRowKeyIdentityUserRole(roleName);
                        DynamicTableEntity tgtDte           = new DynamicTableEntity(targetUserPartitionKey, targetUserRowKey, Constants.ETagWildcard, sourceEntity.Properties);
                        tgtDte.Properties["UserId"]     = new EntityProperty(userId);
                        tgtDte.Properties["KeyVersion"] = new EntityProperty(_keyHelper.KeyVersion);
                        targetUserEntities.Add(tgtDte);

                        //Role index
                        IdentityUserIndex roleIndex = new IdentityUserIndex()
                        {
                            Id           = targetUserPartitionKey,
                            PartitionKey = targetUserRowKey,
                            RowKey       = targetUserPartitionKey,
                            KeyVersion   = _keyHelper.KeyVersion,
                            ETag         = Constants.ETagWildcard
                        };
                        targetUserIndexes.Add(roleIndex);
                        continue;
                    }
                    //User Token record
                    if (sourceEntity.RowKey.StartsWith(Constants.RowKeyConstants.PreFixIdentityUserToken))
                    {
                        //New User Token
                        sourceEntity.Properties.TryGetValue("LoginProvider", out EntityProperty loginProviderProperty);
                        string loginProvider = loginProviderProperty.StringValue;
                        sourceEntity.Properties.TryGetValue("TokenName", out EntityProperty tokenNameProperty);
                        string tokenName = tokenNameProperty.StringValue;

                        string             targetUserRowKey = _keyHelper.GenerateRowKeyIdentityUserToken(loginProvider, tokenName);
                        DynamicTableEntity tgtDte           = new DynamicTableEntity(targetUserPartitionKey, targetUserRowKey, Constants.ETagWildcard, sourceEntity.Properties);
                        tgtDte.Properties["UserId"]     = new EntityProperty(userId);
                        tgtDte.Properties["KeyVersion"] = new EntityProperty(_keyHelper.KeyVersion);
                        targetUserEntities.Add(tgtDte);
                        continue;
                    }
                }
            }
            return(targetUserEntities, targetUserIndexes);
        }