private static void LinkRuleGroupToRelyingParty(ManagementService client, RuleGroup ruleGroup, RelyingParty relyingParty)
        {
            Guard.NotNull(() => ruleGroup, ruleGroup);
            Guard.NotNull(() => relyingParty, relyingParty);

            var relyingPartyRuleGroup = new RelyingPartyRuleGroup
                {
                    RuleGroupId = ruleGroup.Id,
                    RelyingParty = relyingParty
                };

            client.AddRelatedObject(relyingParty, "RelyingPartyRuleGroups", relyingPartyRuleGroup);
            client.SaveChanges(SaveChangesOptions.Batch);
        }
        private static void CreateRelyingParty(ManagementService client, string relyingPartyName, string ruleGroupName, string realmAddress, string replyAddress, TokenType tokenType, int tokenLifetime, bool asymmetricTokenEncryptionRequired, out RelyingParty relyingParty)
        {
            // Create Relying Party
            relyingParty = new RelyingParty
                               {
                                   Name = relyingPartyName,
                                   DisplayName = relyingPartyName,
                                   Description = relyingPartyName,
                                   TokenType = tokenType.ToString(),
                                   TokenLifetime = tokenLifetime,
                                   AsymmetricTokenEncryptionRequired = asymmetricTokenEncryptionRequired
                               };

            client.AddObject("RelyingParties", relyingParty);
            client.SaveChanges();

            if (!string.IsNullOrWhiteSpace(ruleGroupName))
            {
                RuleGroup ruleGroup = client.RuleGroups.Where(rg => rg.Name.Equals(ruleGroupName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (ruleGroup == null)
                {
                    ruleGroup = new RuleGroup
                                    {
                                        Name = ruleGroupName
                                    };

                    client.AddToRuleGroups(ruleGroup);
                    client.SaveChanges();
                }

                var relyingPartyRuleGroup = new RelyingPartyRuleGroup
                                                {
                                                    RuleGroupId = ruleGroup.Id,
                                                    RelyingParty = relyingParty
                                                };

                client.AddRelatedObject(relyingParty, "RelyingPartyRuleGroups", relyingPartyRuleGroup);
            }

            // Create the Realm for Relying Party
            var realm = new RelyingPartyAddress
                            {
                                Address = realmAddress,
                                EndpointType = RelyingPartyAddressEndpointType.Realm.ToString(),
                                RelyingParty = relyingParty
                            };

            client.AddRelatedObject(relyingParty, "RelyingPartyAddresses", realm);

            if (!string.IsNullOrEmpty(replyAddress))
            {
                var reply = new RelyingPartyAddress
                                {
                                    Address = replyAddress,
                                    EndpointType = RelyingPartyAddressEndpointType.Reply.ToString(),
                                    RelyingParty = relyingParty
                                };

                client.AddRelatedObject(relyingParty, "RelyingPartyAddresses", reply);
            }

            client.SaveChanges(SaveChangesOptions.Batch);
        }
        public void AddRuleGroup(string name)
        {
            try
            {
                var client = this.CreateManagementServiceClient();

                var ruleGroup = new RuleGroup
                                    {
                                        Name = name
                                    };

                client.AddToRuleGroups(ruleGroup);
                client.SaveChanges();
            }
            catch (Exception ex)
            {
                throw TryGetExceptionDetails(ex);
            }
        }