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
     });
 }
 /// <summary>
 /// Generates the RowKey without setting it on the object.
 /// </summary>
 /// <returns></returns>
 public virtual string PeekRowKey()
 {
     return(KeyHelper.GenerateRowKeyIdentityUserLogin(LoginProvider, ProviderKey));
 }
        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);
        }