static void Main(string[] args) { // Display title and info. Console.WriteLine("ASP.NET Configuration Info"); Console.WriteLine("Type: CommaDelimitedStringCollection"); Console.WriteLine(); // Set the path of the config file. string configPath = "/aspnet"; // Get the Web application configuration object. Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath); // Get the section related object. AuthorizationSection configSection = (AuthorizationSection)config.GetSection("system.web/authorization"); // Get the authorization rule collection. AuthorizationRuleCollection authorizationRuleCollection = configSection.Rules; // <Snippet2> // Create a CommaDelimitedStringCollection object. CommaDelimitedStringCollection myStrCollection = new CommaDelimitedStringCollection(); // </Snippet2> for (int i = 0; i < authorizationRuleCollection.Count; i++) { if (authorizationRuleCollection.Get(i).Action.ToString().ToLower() == "allow") { // <Snippet3> // Add values to the CommaDelimitedStringCollection object. myStrCollection.AddRange( authorizationRuleCollection.Get(i).Users.ToString().Split( ",".ToCharArray())); // </Snippet3> } } Console.WriteLine("Allowed Users: {0}", myStrCollection.ToString()); // <Snippet4> // Count the elements in the collection. Console.WriteLine("Allowed User Count: {0}", myStrCollection.Count); // </Snippet4> // <Snippet5> // Call the Contains method. Console.WriteLine("Contains 'userName1': {0}", myStrCollection.Contains("userName1")); // </Snippet5> // <Snippet6> // Determine the index of an element // in the collection. Console.WriteLine("IndexOf 'userName0': {0}", myStrCollection.IndexOf("userName0")); // </Snippet6> // <Snippet7> // Call IsModified. Console.WriteLine("IsModified: {0}", myStrCollection.IsModified); // </Snippet7> // <Snippet8> // Call IsReadyOnly. Console.WriteLine("IsReadOnly: {0}", myStrCollection.IsReadOnly); // </Snippet8> Console.WriteLine(); Console.WriteLine("Add a user name to the collection."); // <Snippet9> // Insert a new element in the collection. myStrCollection.Insert(myStrCollection.Count, "userNameX"); // </Snippet9> Console.WriteLine("Collection Value: {0}", myStrCollection.ToString()); Console.WriteLine(); Console.WriteLine("Remove a user name from the collection."); // <Snippet10> // Remove an element of the collection. myStrCollection.Remove("userNameX"); // </Snippet10> Console.WriteLine("Collection Value: {0}", myStrCollection.ToString()); // Display and wait Console.ReadLine(); }
public static void Main() { // <Snippet1> // Get the Web application configuration. System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration( "/aspnetTest"); // </Snippet1> // <Snippet2> // Get the section. AuthorizationSection authorizationSection = (AuthorizationSection)configuration.GetSection( "system.web/authorization"); // </Snippet2> // <Snippet3> // Get the authorization rule collection. AuthorizationRuleCollection authorizationRuleCollection = authorizationSection.Rules; // </Snippet3> // <Snippet4> // Create an authorization rule object. AuthorizationRuleAction action = AuthorizationRuleAction.Deny; AuthorizationRule authorizationRule = new System.Web.Configuration.AuthorizationRule(action); // </Snippet4> // <Snippet5> // Create a new 'AuthorizationSection' object. AuthorizationSection newauthorizationSection = new System.Web.Configuration.AuthorizationSection(); // </Snippet5> // <Snippet6> // Using the AuthorizationRuleCollection Add method. // Set the action property. authorizationRule.Action = AuthorizationRuleAction.Allow; // Define the new rule to add to the collection. authorizationRule.Users.Add("userName"); authorizationRule.Roles.Add("admin"); authorizationRule.Verbs.Add("POST"); // Add the new rule to the collection. authorizationSection.Rules.Add(authorizationRule); // </Snippet6> // <Snippet7> // Using the AuthorizationRuleCollection Clear method. authorizationSection.Rules.Clear(); // </Snippet7> // <Snippet8> // Using the AuthorizationRuleCollection RemoveAt method. authorizationRuleCollection.RemoveAt(0); // </Snippet8> // <Snippet9> // Get the rule collection index. System.Int32 ruleIndex = authorizationSection.Rules.IndexOf(authorizationRule); // </Snippet9> // <Snippet10> // Remove the rule from the collection. authorizationSection.Rules.Remove(authorizationRule); // </Snippet10> // <Snippet11> // Using the AuthorizationRuleCollection Set method. // Define the rule to add to the collection. // Define the collection index. System.Int32 rIndex = 0; // Set the rule in the collection. authorizationRuleCollection.Set(rIndex, authorizationRule); // </Snippet11> // <Snippet12> // Show how to access the Rules elements. StringBuilder rules = new StringBuilder(); for (System.Int32 i = 0; i < authorizationSection.Rules.Count - 1; i++) { rules.Append("Action: " + authorizationSection.Rules[i].Action.ToString()); // Get the Verbs. System.Int32 verbsCount = authorizationSection.Rules[i].Verbs.Count; for (System.Int32 v = 0; v < verbsCount; v++) { rules.AppendLine( authorizationSection.Rules[i].Verbs[v]); } // Get the Roles. System.Int32 rolesCount = authorizationSection.Rules[i].Roles.Count; for (System.Int32 r = 0; r < rolesCount; r++) { rules.AppendLine(authorizationSection.Rules[i].Roles[r]); } // Get the Users. System.Int32 usersCount = authorizationSection.Rules[i].Users.Count; for (System.Int32 u = 0; u < usersCount; u++) { rules.AppendLine(authorizationSection.Rules[i].Users[u]); } } // </Snippet12> // <Snippet13> // Using the AuthorizationRuleCollection Get method. AuthorizationRule authRule = authorizationRuleCollection.Get(0); // </Snippet13> }