public void ProcessMigrate(IdentityCloudContext targetContext,
                                   IdentityCloudContext sourceContext,
                                   IList <DynamicTableEntity> claimResults,
                                   int maxDegreesParallel,
                                   Action updateComplete       = null,
                                   Action <string> updateError = null)
        {
            const string KeyVersion = "KeyVersion";

            var claims = claimResults
                         .Where(UserWhereFilter)
                         .ToList();


            var result2 = Parallel.ForEach(claims, new ParallelOptions()
            {
                MaxDegreeOfParallelism = maxDegreesParallel
            }, (claim) =>
            {
                //Add the new claim index
                try
                {
                    var claimNew = new DynamicTableEntity(claim.PartitionKey,
                                                          KeyHelper.GenerateRowKeyIdentityUserClaim(claim.Properties["ClaimType"].StringValue, claim.Properties["ClaimValue"].StringValue),
                                                          Constants.ETagWildcard,
                                                          claim.Properties);
                    if (claimNew.Properties.ContainsKey(KeyVersion))
                    {
                        claimNew.Properties[KeyVersion].DoubleValue = KeyHelper.KeyVersion;
                    }
                    else
                    {
                        claimNew.Properties.Add(KeyVersion, EntityProperty.GeneratePropertyForDouble(KeyHelper.KeyVersion));
                    }

                    var taskExecute = targetContext.UserTable.ExecuteAsync(TableOperation.InsertOrReplace(claimNew));
                    taskExecute.Wait();

                    updateComplete?.Invoke();
                }
                catch (Exception ex)
                {
                    updateError?.Invoke(string.Format("{0}\t{1}", claim.PartitionKey, ex.Message));
                }
            });
        }
Esempio n. 2
0
 /// <summary>
 /// Generates the RowKey without setting it on the object.
 /// </summary>
 /// <returns></returns>
 public string PeekRowKey()
 {
     return(KeyHelper.GenerateRowKeyIdentityUserClaim(ClaimType, ClaimValue));
 }
        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);
        }