public void Defaults() { AuthorizationSection a = new AuthorizationSection (); Assert.IsNotNull (a.Rules, "A1"); Assert.AreEqual (0, a.Rules.Count, "A2"); }
protected void AddFirstGroupOfRules(AuthorizationSection section, ArrayList rulesArray, int selectedIndex, string upOrDown) { int adj; if (upOrDown == "up") adj = 1; else adj = 0; for (int x = 0; x < selectedIndex - adj; x++) { section.Rules.Add((AuthorizationRule)rulesArray[x]); } }
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> }
protected void AddTheTwoSwappedRules(AuthorizationSection section, ArrayList rulesArray, int selectedIndex, string upOrDown) { if (upOrDown == "up") { section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex]); section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex - 1]); } else if (upOrDown == "down") { section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex + 1]); section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex]); } }
protected ArrayList PullLocalRulesOutOfAuthorizationSection(AuthorizationSection section) { // First load the local rules into an ArrayList. ArrayList rulesArray = new ArrayList(); foreach (AuthorizationRule rule in section.Rules) { if (rule.ElementInformation.IsPresent) { rulesArray.Add(rule); } } // Next delete the rules from the section. foreach (AuthorizationRule rule in rulesArray) { section.Rules.Remove(rule); } return rulesArray; }
protected void LoadRulesInNewOrder(AuthorizationSection section, ArrayList rulesArray, int selectedIndex, string upOrDown) { AddFirstGroupOfRules(section, rulesArray, selectedIndex, upOrDown); AddTheTwoSwappedRules(section, rulesArray, selectedIndex, upOrDown); AddFinalGroupOfRules(section, rulesArray, selectedIndex, upOrDown); }
private string GetTheRuleForThisUser(AuthorizationSection section, string folder) { foreach (AuthorizationRule rule in section.Rules) { foreach (string user in rule.Users) { if (user == "*") { return rule.Action.ToString().ToUpper() + ": All Users"; } else if (user == selectedUser) { return rule.Action.ToString().ToUpper() + ": User="******": Role=" + role; } } } return "ALLOW"; }
private string GetTheRuleForThisRole(AuthorizationSection section, string folder) { foreach (AuthorizationRule rule in section.Rules) { foreach (string user in rule.Users) { if (user == "*") { return rule.Action.ToString().ToUpper() + ": All Users"; } } foreach (string role in rule.Roles) { if (role == selectedRole) { return rule.Action.ToString().ToUpper() + ": Role=" + role; } } } return "Allow"; }
/// <summary> /// Initializes a module and prepares it to handle requests. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpApplication" /> that provides access to the methods, properties, /// and events common to all application objects within an ASP.NET application</param> public void Init(HttpApplication context) { context.AuthorizeRequest += new EventHandler(this.AuthorizeRequestHandler); Configuration.RootSection root = (Configuration.RootSection)ConfigurationManager.GetSection("consolehost.web"); this.config = root.Authorization; }