title: How can I modify the following code to pull data from a database?
body: <p>I am using the following asp.net mvc filter to filter ips. Currently, you can specify the ip(s) in code directly or in a configuration file.  How can I modify it to pull the denied or allowed ips from a database and use that in the filter?  Here is the code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Security.Principal;
  using System.Configuration;
  
  namespace Miscellaneous.Attributes.Controller
  {
  
  /// &lt;summary&gt;
  /// Filter by IP address
  /// &lt;/summary&gt;
  public class FilterIPAttribute : AuthorizeAttribute
  {
  
      #region Allowed
      /// &lt;summary&gt;
      /// Comma seperated string of allowable IPs. Example "10.2.5.41,192.168.0.22"
      /// &lt;/summary&gt;
      /// &lt;value&gt;&lt;/value&gt;
      public string AllowedSingleIPs { get; set; }
  
      /// &lt;summary&gt;
      /// Comma seperated string of allowable IPs with masks. Example "10.2.0.0;255.255.0.0,10.3.0.0;255.255.0.0"
      /// &lt;/summary&gt;
      /// &lt;value&gt;The masked I ps.&lt;/value&gt;
      public string AllowedMaskedIPs { get; set; }
  
      /// &lt;summary&gt;
      /// Gets or sets the configuration key for allowed single IPs
      /// &lt;/summary&gt;
      /// &lt;value&gt;The configuration key single I ps.&lt;/value&gt;
      public string ConfigurationKeyAllowedSingleIPs { get; set; }
  
      /// &lt;summary&gt;
      /// Gets or sets the configuration key allowed mmasked IPs
      /// &lt;/summary&gt;
      /// &lt;value&gt;The configuration key masked I ps.&lt;/value&gt;
      public string ConfigurationKeyAllowedMaskedIPs { get; set; }
  
      /// &lt;summary&gt;
      /// List of allowed IPs
      /// &lt;/summary&gt;
      IPList allowedIPListToCheck = new IPList();
      #endregion
  
      #region Denied
      /// &lt;summary&gt;
      /// Comma seperated string of denied IPs. Example "10.2.5.41,192.168.0.22"
      /// &lt;/summary&gt;
      /// &lt;value&gt;&lt;/value&gt;
      public string DeniedSingleIPs { get; set; }
  
      /// &lt;summary&gt;
      /// Comma seperated string of denied IPs with masks. Example "10.2.0.0;255.255.0.0,10.3.0.0;255.255.0.0"
      /// &lt;/summary&gt;
      /// &lt;value&gt;The masked I ps.&lt;/value&gt;
      public string DeniedMaskedIPs { get; set; }
  
  
      /// &lt;summary&gt;
      /// Gets or sets the configuration key for denied single IPs
      /// &lt;/summary&gt;
      /// &lt;value&gt;The configuration key single I ps.&lt;/value&gt;
      public string ConfigurationKeyDeniedSingleIPs { get; set; }
  
      /// &lt;summary&gt;
      /// Gets or sets the configuration key for denied masked IPs
      /// &lt;/summary&gt;
      /// &lt;value&gt;The configuration key masked I ps.&lt;/value&gt;
      public string ConfigurationKeyDeniedMaskedIPs { get; set; }
  
      /// &lt;summary&gt;
      /// List of denied IPs
      /// &lt;/summary&gt;
      IPList deniedIPListToCheck = new IPList();
      #endregion
  
  
      /// &lt;summary&gt;
      /// Determines whether access to the core framework is authorized.
      /// &lt;/summary&gt;
      /// &lt;param name="httpContext"&gt;The HTTP context, which encapsulates all HTTP-specific information about an individual HTTP request.&lt;/param&gt;
      /// &lt;returns&gt;
      /// true if access is authorized; otherwise, false.
      /// &lt;/returns&gt;
      /// &lt;exception cref="T:System.ArgumentNullException"&gt;The &lt;paramref name="httpContext"/&gt; parameter is null.&lt;/exception&gt;
      protected override bool AuthorizeCore(HttpContextBase httpContext)
      {
          if (httpContext == null)
              throw new ArgumentNullException("httpContext");
  
          string userIpAddress = httpContext.Request.UserHostAddress;
  
          try
          {
              // Check that the IP is allowed to access
              bool ipAllowed = CheckAllowedIPs(userIpAddress);
  
              // Check that the IP is not denied to access
              bool ipDenied = CheckDeniedIPs(userIpAddress);    
  
              // Only allowed if allowed and not denied
              bool finallyAllowed = ipAllowed &amp;&amp; !ipDenied;
  
              return finallyAllowed;
          }
          catch (Exception e)
          {
              // Log the exception, probably something wrong with the configuration
          }
  
          return true; // if there was an exception, then we return true
      }
  
      /// &lt;summary&gt;
      /// Checks the allowed IPs.
      /// &lt;/summary&gt;
      /// &lt;param name="userIpAddress"&gt;The user ip address.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private bool CheckAllowedIPs(string userIpAddress)
      {
          // Populate the IPList with the Single IPs
          if (!string.IsNullOrEmpty(AllowedSingleIPs))
          {
              SplitAndAddSingleIPs(AllowedSingleIPs, allowedIPListToCheck);
          }
  
          // Populate the IPList with the Masked IPs
          if (!string.IsNullOrEmpty(AllowedMaskedIPs))
          {
              SplitAndAddMaskedIPs(AllowedMaskedIPs, allowedIPListToCheck);
          }
  
          // Check if there are more settings from the configuration (Web.config)
          if (!string.IsNullOrEmpty(ConfigurationKeyAllowedSingleIPs))
          {
              string configurationAllowedAdminSingleIPs = ConfigurationManager.AppSettings[ConfigurationKeyAllowedSingleIPs];
              if (!string.IsNullOrEmpty(configurationAllowedAdminSingleIPs))
              {
                  SplitAndAddSingleIPs(configurationAllowedAdminSingleIPs, allowedIPListToCheck);
              }
          }
  
          if (!string.IsNullOrEmpty(ConfigurationKeyAllowedMaskedIPs))
          {
              string configurationAllowedAdminMaskedIPs = ConfigurationManager.AppSettings[ConfigurationKeyAllowedMaskedIPs];
              if (!string.IsNullOrEmpty(configurationAllowedAdminMaskedIPs))
              {
                  SplitAndAddMaskedIPs(configurationAllowedAdminMaskedIPs, allowedIPListToCheck);
              }
          }
  
          return allowedIPListToCheck.CheckNumber(userIpAddress);
      }
  
      /// &lt;summary&gt;
      /// Checks the denied IPs.
      /// &lt;/summary&gt;
      /// &lt;param name="userIpAddress"&gt;The user ip address.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private bool CheckDeniedIPs(string userIpAddress)
      {
          // Populate the IPList with the Single IPs
          if (!string.IsNullOrEmpty(DeniedSingleIPs))
          {
              SplitAndAddSingleIPs(DeniedSingleIPs, deniedIPListToCheck);
          }
  
          // Populate the IPList with the Masked IPs
          if (!string.IsNullOrEmpty(DeniedMaskedIPs))
          {
              SplitAndAddMaskedIPs(DeniedMaskedIPs, deniedIPListToCheck);
          }
  
          // Check if there are more settings from the configuration (Web.config)
          if (!string.IsNullOrEmpty(ConfigurationKeyDeniedSingleIPs))
          {
              string configurationDeniedAdminSingleIPs = ConfigurationManager.AppSettings[ConfigurationKeyDeniedSingleIPs];
              if (!string.IsNullOrEmpty(configurationDeniedAdminSingleIPs))
              {
                  SplitAndAddSingleIPs(configurationDeniedAdminSingleIPs, deniedIPListToCheck);
              }
          }
  
          if (!string.IsNullOrEmpty(ConfigurationKeyDeniedMaskedIPs))
          {
              string configurationDeniedAdminMaskedIPs = ConfigurationManager.AppSettings[ConfigurationKeyDeniedMaskedIPs];
              if (!string.IsNullOrEmpty(configurationDeniedAdminMaskedIPs))
              {
                  SplitAndAddMaskedIPs(configurationDeniedAdminMaskedIPs, deniedIPListToCheck);
              }
          }
  
          return deniedIPListToCheck.CheckNumber(userIpAddress);
      }
  
      /// &lt;summary&gt;
      /// Splits the incoming ip string of the format "IP,IP" example "10.2.0.0,10.3.0.0" and adds the result to the IPList
      /// &lt;/summary&gt;
      /// &lt;param name="ips"&gt;The ips.&lt;/param&gt;
      /// &lt;param name="list"&gt;The list.&lt;/param&gt;
      private void SplitAndAddSingleIPs(string ips,IPList list)
      {
          var splitSingleIPs = ips.Split(',');
          foreach (string ip in splitSingleIPs)
              list.Add(ip);
      }
  
      /// &lt;summary&gt;
      /// Splits the incoming ip string of the format "IP;MASK,IP;MASK" example "10.2.0.0;255.255.0.0,10.3.0.0;255.255.0.0" and adds the result to the IPList
      /// &lt;/summary&gt;
      /// &lt;param name="ips"&gt;The ips.&lt;/param&gt;
      /// &lt;param name="list"&gt;The list.&lt;/param&gt;
      private void SplitAndAddMaskedIPs(string ips, IPList list)
      {
          var splitMaskedIPs = ips.Split(',');
          foreach (string maskedIp in splitMaskedIPs)
          {
              var ipAndMask = maskedIp.Split(';');
              list.Add(ipAndMask[0], ipAndMask[1]); // IP;MASK
          }
      }
  
      public override void OnAuthorization(AuthorizationContext filterContext)
      {
          base.OnAuthorization(filterContext);
      }
  }
  }
  </code></pre>
  
  <p>Example usage:</p>
  
  <ol>
  <li><p>Directly specifying the IPs in the code</p>
  
  <pre><code> [FilterIP(
      AllowedSingleIPs="10.2.5.55,192.168.2.2",
      AllowedMaskedIPs="10.2.0.0;255.255.0.0,192.168.2.0;255.255.255.0"
  </code></pre>
  
  <p>)]</p>
  
  <pre><code>public class HomeController 
  {
  // Some code here
  }
  </code></pre></li>
  <li><p>Or, Loading the configuration from the Web.config</p>
  
  <pre><code> [FilterIP(
      ConfigurationKeyAllowedSingleIPs="AllowedAdminSingleIPs",
      ConfigurationKeyAllowedMaskedIPs="AllowedAdminMaskedIPs",
      ConfigurationKeyDeniedSingleIPs="DeniedAdminSingleIPs",
      ConfigurationKeyDeniedMaskedIPs="DeniedAdminMaskedIPs"
  </code></pre>
  
  <p>)]</p>
  
  <pre><code>&lt;configuration&gt;
  &lt;appSettings&gt;
  &lt;add key="AllowedAdminSingleIPs" value="localhost,127.0.0.1"/&gt; &lt;!-- Example "10.2.80.21,192.168.2.2" --&gt;
  &lt;add key="AllowedAdminMaskedIPs" value="10.2.0.0;255.255.0.0"/&gt; &lt;!-- Example "10.2.0.0;255.255.0.0,192.168.2.0;255.255.255.0" --&gt;
  &lt;add key="DeniedAdminSingleIPs" value=""/&gt;    &lt;!-- Example "10.2.80.21,192.168.2.2" --&gt;
  &lt;add key="DeniedAdminMaskedIPs" value=""/&gt;    &lt;!-- Example "10.2.0.0;255.255.0.0,192.168.2.0;255.255.255.0" --&gt;
  &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre></li>
  </ol>